What does the ! operator do?

646 views
Skip to first unread message

Peter Bruinsma

unread,
May 15, 2016, 6:21:18 PM5/15/16
to Elm Discuss
For example, in TodoMVC:

init : Maybe Model -> ( Model, Cmd Msg )
init savedModel =
  Maybe.withDefault emptyModel savedModel ! []








Joey Eremondi

unread,
May 15, 2016, 6:29:21 PM5/15/16
to elm-d...@googlegroups.com
Not documented, but at least the type might help figure it out?









--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nandiin Bao

unread,
May 15, 2016, 10:09:06 PM5/15/16
to Elm Discuss
I've looked at compiled javascript code and it's saying something like this :

(!) = (\model list -> (model, Cmd.batch list))


在 2016年5月16日星期一 UTC+8上午6:21:18,Peter Bruinsma写道:

Peter Damoc

unread,
May 16, 2016, 1:45:25 AM5/16/16
to Elm Discuss
This is exactly what it is. 

It takes a value and a list and returns a tuple with the value and the command resulted from batching that list. 

I used to have a similar function back in 0.16 days 

noFx model = (model, Effects.none) 

and I would have used it like 

SomeAction -> 
    noFx {model |...} 

because I liked it better than 

SomeAction -> 
    ({model | ... }, Effects.none) 
 

but this new way is way better. 

If you don't have Cmd to return, you just leave the list empty and if you do have some Cmds (e.g. from children updates) you can just add them in the list. 




--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Peter Bruinsma

unread,
May 16, 2016, 1:33:53 PM5/16/16
to Elm Discuss
Thanks all. I'm still somewhat confused.

The information architecture of the elm website is certainly not helping :)

One would expect info about an operator to be somewhere under elm-lang.org/docs/*

However, after reading http://package.elm-lang.org/packages/elm-lang/core/4.0.0/Platform-Cmd it appears that (!) is not an "all-purpose" operator.

Since there isn't any further explanation regarding (!) on this page (apart from the type description), I decided to see if there is anything else on this page that can help me understand Cmd (in general)

Here we go:
"Note: Do not worry if this seems confusing at first! As with every Elm user ever, commands will make more sense as you work through the Elm Architecture Tutorial and see how they fit into a real application!"

Unfortunately, that link goes nowhere. Fortunately, I know where to find the Elm Architecture Tutorial (now just a chapter in the guide under another subdomain because it's a gitbook...). 

Nowhere in the guide is there a clear explanation of what a Cmd, a Msg or a Sub is. (Oh, a Msg is not anything. It's just a user defined type. But in all the examples it follows the same abbreviation pattern as Cmd and Sub, so it looks like an official thing. Very confusing for a beginner!)

Anyway, I typed the following in the REPL:

> "" ! []

("",{ type = "node", branches = [] }) : ( String, Platform.Cmd.Cmd a )


Not sure what to do with that...

I guess this is not really a question anymore. I'm just venting a bit of frustration :)

/Peter

Liam Curry

unread,
May 17, 2016, 2:02:39 PM5/17/16
to Elm Discuss
A Cmd is pretty much the same as an Effect from 0.16.

"!" is an infix operator that is pretty much just an alias for "Cmd.batch" (or Effects.batch from 0.16). Here's the source code for !:

{-|-}
(!) : model -> List (Cmd msg) -> (model, Cmd msg)
(!) model commands =
  (model, batch commands)

So instead of having to write something like this:

(model, Cmd.batch [ cmd1, cmd2, cmd3 ])

You can write:

model ! [ cmd1, cmd2, cmd3 ]

And instead of this:

(model, Cmd.none)

You can write:

model ! []
Reply all
Reply to author
Forward
0 new messages