Elm - Include Msg in Cmd Msg

I am trying to change a simple application from the elm-lang tutorial to update the model first and then run another update.

update msg model = case msg of MorePlease -> (model, getRandomGif model.topic) NewGif (Ok newUrl) -> ( { model | gifUrl = newUrl }, Cmd.none) NewGif (Err _) -> (model, Cmd.none) -- my addition NewTopic newTopic -> ({ model | topic = newTopic}, MorePlease) 

This does not work in the compiler, because the NewTopic branch:

 The 3rd branch has this type: ( { gifUrl : String, topic : String }, Cmd Msg ) But the 4th is: ( { gifUrl : String, topic : String }, Msg ) 

So, my Msg should be type Cmd Msg. How can I turn "Msg" into Cmd Msg?

note: I understand that there is an easy way to make this change, but I'm trying to understand Elm more fundamentally

+7
elm
source share
1 answer

There is no need to turn Msg into Cmd Msg . Remember that update is just a function, so you can call it recursively.

Your case NewTopic handler can be simplified:

 NewTopic newTopic -> update MorePlease { model | topic = newTopic} 

If you really wanted Elm Architecture to miss Cmd for this scenario, you can make a simple map from Cmd.none to your desired Msg :

 NewTopic newTopic -> ({ model | topic = newTopic}, Cmd.map (always MorePlease) Cmd.none) 

(actually not recommended)

+15
source share

All Articles