You could invert your cases.

update msg state =
    case state of
           Loading ->
                  case msg of
                        (all your messages that apply when loading)
           Displaying stuff ->
                   case msg of
                        (all your messages that apply when displaying




On Tuesday, January 17, 2017 at 9:03:05 AM UTC-5, Tomáš Znamenáček wrote:
>
> Hello!
>
> I have an app that loads some JSON from a website and displays it. The 
> state of the app could be modelled a bit like this:
>
> type State =
>     Loading |
>     Displaying Stuff
>
> type alias Stuff = { … }
>
> Now I am writing the update function to handle messages:
>
> update : Msg -> State -> (State, Cmd Msg)
> update msg state = case msg of
>     UpdateThis -> …
>     UpdateThat -> …
>
> My problem is that the state can be either Loading or Displaying, but most 
> of the messages only make sense in the Displaying state, leading me to code 
> like this:
>
> update : Msg -> State -> (State, Cmd Msg)
> update msg state = case msg of
>     UpdateThis -> case state of
>         Displaying stuff -> …
>         _ -> (Failed "Invalid state", Cmd.none)
>     UpdateThat -> case state of
>         Displaying stuff -> …
>         _ -> (Failed "Invalid state", Cmd.none)
>
> Obviously, this is dumb. How can I “divide” the loading and the displaying 
> part of the app? (Assuming that UpdateThis and UpdateThat can only arrive 
> in the correct, Displaying state.)
>
> Thank you!
>
> T.
>

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

Reply via email to