What does “! []” Elm code syntax in Todomvc mean

白昼怎懂夜的黑 提交于 2019-12-18 11:38:50

问题


Coming from react, I am learning to understand Elm.

In the Todomvc example code, there is the following code snippet:

-- How we update our Model on a given Msg?
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
  case msg of
    NoOp ->
      model ! []        <-- What is this?

What I (think I) understand, is that the update function takes in a msg of type Msg and a model of type Model, and returns a tuple of containing a Model and a Cmd Msg.

But how should I read the return statement?

model ! []

What does this statement mean? return a "model [something] empty list"?
Did I miss something in the docs where this is explained? (Googling "elm !" did not get me far :)


回答1:


Update for Elm 0.19

Elm 0.19 has removed the exclamation point operator. You must now construct the tuple manually, as in (model, Cmd.none).

Original Answer for Elm 0.18

The exclamation point in model ! [] is just a short-hand function for (model, Cmd.batch []), which is the type returned from typical update statements. It is defined here




回答2:


Note that this syntax is going away in the next version of Elm (0.19) so don't get into the habit of using it ;-)

You can use today, and with 0.19:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
  case msg of
    NoOp ->
      (model, Cmd.none)


来源:https://stackoverflow.com/questions/37448159/what-does-elm-code-syntax-in-todomvc-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!