I’m new to Elm and really enjoying it. Naturally this has led to me evangelising about it to anyone that will listen, and ultimately it’s ended up with me being roped into giving a talk on Elm at my local meetup!
I’ve come up with a plan which involves slowly building an app, introducing concepts as I go. I’ll give the audience non-compiling samples in Ellie, which they’ll need to complete to show that they’ve understood each concept.
I wanted to post the app so far in case anyone with more experience in Elm could spot areas where the code isn’t ideal:
In particular I don’t feel totally happy about the update function, as I’m having to cover off a case that in reality should be impossible. I’m not sure there’s any way around this though as there’s no way for the compiler to know that certain model types are restricted to certain message types.
update msg model =
case msg of
Select card ->
case model of
CardList cards _ ->
( CardList cards (Just card), Cmd.none )
-- this case should be impossible, because only the CardList view has Select buttons
_ ->
( model, Cmd.none )
Loaded (Result.Ok cards) ->
( CardList cards Nothing, Cmd.none )
Loaded (Result.Err err) ->
case err of
BadStatus { body } ->
( Error body, Cmd.none )
_ ->
( Error "Unknown error", Cmd.none )
Ah it’s only really the Select case that needs to worry about different model cases, and that’s only because I need to keep the cards data after the update.
I did try tupling them and doing case (model, msg) of but it didn’t feel much better, and I thought it might be harder for them to understand.
Thanks Ryan, that definitely makes the the update function read better! I’d need to tweak my slides in order to introduce the { record | field = val } syntax before this point but that’s maybe not a bad thing!
I guess I was obsessing too much about “make illegal states unrepresentable”, i.e. making it impossible to construct a model where the cards are still loading and there’s a card selected. I could make that part of the discussion though.
My audience will be mostly experienced OOP devs (C# devs who reluctantly write JS). My main goal for the talk is to show them that with Elm/FP it’s possible to build apps without null, this, exceptions or hidden side-effects - i.e. almost completely avoid runtime errors. I think that will be the thing that will sell it to them