TL;DR: I seem to be hand-coding method dispatch. Is that a useful Elm
idiom/pattern?
——
In my `update` function, I found I had a number of messages that all took the
same “arguments”. For example, there were a number of functions that all took a
`DisplayedAnimal` value plus perhaps some other values.
It also happened that all those messages shared common behavior. In the above
case, each displayed animal has a Rails-like informational “flash” that should
be shown until the user manipulates the animal. (So animal-related Msgs should
erase the flash, and all others should leave them alone.)
That led me to collapsing all Msgs that operate on a displayed animal into
their own sum type. And so my main `update` delegated all such values to code
like this:
> animalOp : AnimalOperation -> Animal.DisplayedAnimal -> Model -> (Model, Cmd
> Msg)
> animalOp op displayed model =
> animalOp_ op (noFlash displayed) model
>
> animalOp_ op displayed model =
> case op of
> SwitchToReadOnly format -> ...
> StartEditing -> ...
As is usual, all the variants have to be introduced by a particular token that
the `update` case can switch on:
> update msg model =
> case msg of
> ...
> <TOKEN> displayedAnimal more ->
> animalOp more displayedAnimal model
What’s interesting is the name I ended up picking for <TOKEN> is `WithAnimal`,
which leads to client code like this:
> expand displayed iconType =
> iconType "fa-caret-down"
> "Expand: show more about this animal"
> (WithAnimal displayed <| SwitchToReadOnly Expanded) — <<<<=====
>
> contract displayed iconType =
> iconType "fa-caret-up"
> "Contract: show less about this animal"
> (WithAnimal displayed <| SwitchToReadOnly Compact) — <<<<=====
>
> edit displayed iconType =
> iconType "fa-pencil"
> "Edit: make changes to this animal"
> (WithAnimal displayed StartEditing) —
> <<<<=====
Frankly, this:
WithAnimal displayed SwitchToReadOnly Expanded
... looks like a fairly trivial variation of what you’d write, in Ruby, as:
displayed.switch_to_read_only :expanded`
… or, more exactly:
`displayed.send :switch_to_read_only, :expanded
… which makes me nervous. So: what do more experienced Elm programmers think?
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.