OK, or very very wrong?

128 views
Skip to first unread message

Marshall handheld Flax

unread,
Jan 17, 2017, 10:24:10 PM1/17/17
to Elm Discuss
Is it wrong for Cmds to contain functions (as opposed to data within constructors)?  If it is a reasonable practice, it would allow for more functional component-like modules, but does this violate the Elm architecture?  If it does, is it explicitly mentioned in the docs -- I don't remember seeing it. Here's http://elm-lang.org/examples/buttons rewritten in the Cmd-contains-functions style. Thank you!

module Main exposing (..)

import Html exposing (beginnerProgram, div, button, text)
import Html.Events exposing (onClick)

main
=
    beginnerProgram
{ model = 0, view = view, update = update }

type
Msg
   
= Transform (Int -> Int)

view model
=
    div
[]
       
[ button [ onClick (Transform ((+) -1)) ] [ text "-" ]
       
, div [] [ text (toString model) ]
       
, button [ onClick (Transform ((+) 1)) ] [ text "+" ]
       
]

update msg model
=
   
case msg of
       
Transform xform ->
            xform model


Cristian Garcia

unread,
Jan 17, 2017, 11:02:33 PM1/17/17
to Elm Discuss

A part of me likes this since it can help simplify some code, like in this example where the "update" is reduced to a single case. You can even simplify it to a trivial line if you define Msg like this:
-----------------------
view model =
  div []
    [ button [ onClick <| (+) -1 ] [ text "-" ]


    , div [] [ text (toString model) ]

    , button [ onClick <| (+) 1 ] [ text "+" ]
    ]

type alias Msg = Int -> Int

update msg model = msg model
-----------------------
On the other hand defining the state logic from the view seems like an antipattern since now the view now has 2 responsabilities (structure and logic) and update practically has none. I'd be pragmatic and use it when it makes sense, but in general I see the disadvantage that you can pattern match against a function argument, e.g. you cant know if "(+) -1" or "(+) 1" was sent.


--
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.

Nick H

unread,
Jan 18, 2017, 2:31:55 AM1/18/17
to elm-d...@googlegroups.com
I would come down on the side of very very wrong. Functions can't be converted into strings, and they can't be checked for equality. So if you need to debug your program (say, with the interactive debugger), There is no way to examine the data being passed in your commands.


--
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+unsubscribe@googlegroups.com.

Nick H

unread,
Jan 18, 2017, 2:37:00 AM1/18/17
to elm-d...@googlegroups.com
Your particular example can be rewritten easily to not use functions (and with even less duplication):

type Msg
    = Add Int

view model
=
    div
[]
       
[ button [ onClick (Add -1) ] [ text "-" ]

       
, div [] [ text (toString model) ]

       
, button [ onClick (Add 1) ] [ text "+" ]

       
]

update msg model
=
   
case msg of
        Add delta ->
            model + delta

Marshall handheld Flax

unread,
Jan 18, 2017, 5:53:17 AM1/18/17
to Elm Discuss
Even worse, it blocks the import/export feature of the time-travelling debugger -- see ./elm-stuff/packages/elm-lang/virtual-dom/2.0.3/src/VirtualDom/Overlay.elm which emits the error "type of your program cannot be reliably serialized for history files." 

I'm wondering: should the compiler issue a warning if I do something this wrong?

Thanks!

Marshall
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.

Joel Clermont

unread,
Jan 18, 2017, 9:01:20 AM1/18/17
to Elm Discuss
Here is some pretty direct guidance in the elm-sortable-table docs

One of the core rules of The Elm Architecture is never put functions in your Model or Msg types. It may cost a little bit of extra code to model everything as data, but the architecture and debugging benefits are worth it.
Reply all
Reply to author
Forward
0 new messages