57

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 :)

wintvelt
  • 13,855
  • 3
  • 38
  • 43

2 Answers2

70

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

Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
  • 1
    Thanks for explaining. I did come across the cryptic `(!) : model -> List (Cmd msg) -> (model, Cmd msg)` line, but I couldn't translate it to `model ! []` Hopefully more docs will follow. Feels like a promising language so far. – wintvelt May 25 '16 at 22:09
  • 11
    That's one of the major problem with elm. How one is going to search on the web an exclamation mark "!"? I tried searching around ten variations of "elm-lang exclamation mark". I even searched "haskell exclamation mark". Why can't we write `(model, Cmd.batch [])` as it is? It makes it lot less magical. Even if you have to make it "less cody", then shouldn't infix operator with a word be used like ``someOperator``? Just some thoughts. – abhinav Sep 20 '16 at 13:19
  • 13
    You can use the Elm Packages Fancy Search (Go to [Elm Packages](http://package.elm-lang.org/) and click on [Fancy Search](http://klaftertief.github.io/elm-search/)) to find functions like that. [Here you can search on `!`](http://klaftertief.github.io/elm-search/?q=!) and it pulls up the correct function. – Chad Gilbert Sep 20 '16 at 13:23
  • Oh great. Didn't know about that. That helps! Thanks a lot. – abhinav Sep 20 '16 at 13:25
  • 5
    You Elm folks are so freaking quick. :) – abhinav Sep 20 '16 at 13:26
  • If `!` is defined `(!) : model -> List (Cmd msg) -> (model, Cmd msg`, why calling it like `model ! []` rather then `! model []`? Why can some functions appear between the arguments and others cannot? – Chris.Zou Nov 17 '16 at 21:48
  • 1
    @Chris.Zou because some functions can be used between the 2 arguments (infix is the term). So `(!) model []` is the same as `model ! []`. Most basic examples are math functions like `+` etc. I think if the function is defined between parentheses, then you can use them between the two arguments. – wintvelt Nov 18 '16 at 09:47
  • 3
    FWIW I found [this discussion](https://groups.google.com/d/msg/elm-discuss/AXJdAKf4Mbg/MSuw9Y4NCwAJ) helpful. – Inactivist Nov 18 '16 at 14:33
8

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)
Simon H
  • 20,332
  • 14
  • 71
  • 128