# Thoughts on an update wrapper?

**URL:** <https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030>\
**Category:** Request Feedback\
**Created:** [October 28, 2024, 3:00pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030 "2024-10-28T15:00:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamyakes](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/adamyakes/32/4385_2.png) [@adamyakes](https://discourse.elm-lang.org/u/adamyakes)\
**Post date:** [October 28, 2024, 3:00pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/1 "2024-10-28T15:00:00Z")

</div>

I sometimes find myself in a situation where 99% of the time I don’t require a `Cmd msg`, but for one or two cases I do. So I end up writing code like this

```elm
type Msg
  = M1
  | M2
  | M3
  -- ...
  | Refresh

update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
  case msg of
    M1 ->
      -- ...
      ( newModel, Cmd.none )
    M2 ->
      -- ...
      ( newModel, Cmd.none )
    M3 ->
      -- ...
      ( newModel, Cmd.none )
    -- ...
    Refresh ->
      -- ...
      ( newModel, oneOfTheOnlyCommandsIUse )

```

The tuples just feel awkward to me, so I tried this with a lambda that unwraps it in my call to `Browser.application`:

```elm
type Update model msg = NewModel model | WithEffect model msg

update : Msg -> Model -> Update Model Msg 
update msg model =
  case msg of
    M1 ->
      -- ...
      NewModel newModel
    M2 ->
      -- ...
      NewModel newModel
    M3 ->
      -- ...
      NewModel newModel
    -- ...
    Refresh ->
      -- ...
      WithEffect newModel oneOfTheOnlyCommandsIUse

```

To me this shines with record updates.

```elm
NewModel { model | someField = newValue }
-- vs
( { model | someField = newValue }, Cmd.none )

```

Technically, it’s about as repetitive as before, but to me it just _feels_ nicer to me. What do you all think? Little quality of life improvement, ugly, not necessary, have something better?

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [October 28, 2024, 3:19pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/2 "2024-10-28T15:19:19Z")

</div>

This is a fairly common approach from what I’ve seen. We use it at work and I use it in my side projects. There’s a handful of packages for this as well, e.g. [elm-update-helper 2.3.0](https://package.elm-lang.org/packages/the-sett/elm-update-helper/latest/), [elm-update-pipeline 1.3.2](https://package.elm-lang.org/packages/laserpants/elm-update-pipeline/latest/), [return 1.0.3](https://package.elm-lang.org/packages/Chadtech/return/latest/), and a handful more if you search for “update”.

---

<div class="post-metadata">

**Author:** ![nateabele](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nateabele/32/1504_2.png) [@nateabele](https://discourse.elm-lang.org/u/nateabele)\
**Post date:** [October 28, 2024, 8:57pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/3 "2024-10-28T20:57:19Z")

</div>

Yeah, I’ve been in the same boat for a lot of my apps, but I usually just carry around a helper library with stuff like this:

```elm
done : a -> ( a, Cmd msg )
done =
    flip Tuple.pair Cmd.none

```

Then I can just do:

```auto
update : Msg -> Model -> Update Model Msg 
update msg model =
  case msg of
    M1 ->
      -- ...
      done newModel
    M2 ->
      -- ...
      newModel |> someFn |> done
  -- ... etc.

```

(I also have `do` for when I _only_ need to run commands and don’t need any model changes).

Feels more composable and less noisy than a separate type, but, whatever works for ya. 😁

---

<div class="post-metadata">

**Author:** ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)\
**Post date:** [October 28, 2024, 9:29pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/4 "2024-10-28T21:29:25Z")

</div>

Does anyone have/use a variant where they can specify something like `NoModelChange`, without having to pass in the original `Model`? Example:

```elm
type Update model msg
   = NoChange
   | NewModel model
   | Effect msg
   | NewModelAndEffect model msg

update : Msg -> Model -> Update Model Msg 
update msg model =
  case msg of
    Increment ->
      NewModel (increment model)

    TextFieldReceivedFocus ->
      NoChange

    UserClickedSaveButton ->
      let
          newModel : Model
          newModel =
             { model | httpRequestInProgress = True }
      in
      NewModelAndEffect (sendHttpRequest model)

    SaveFinished ->
      NewModel { model | httpRequestInProgress = False }

```

A nice benefit of this is that if the model doesn’t change when we use `NoChange`, then we don’t need to update the model either in the “parent” module, which can be beneficial to avoid unnecessary HTML lazy failures.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 28, 2024, 10:39pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/5 "2024-10-28T22:39:24Z")

</div>

> [@jfmengels](#):
>
> A nice benefit of this is that if the model doesn’t change when we use `NoChange`, then we don’t need to update the model either in the “parent” module, which can be beneficial to avoid unnecessary HTML lazy failures.

Same with your `Effect msg` variant, since that also does not change the model?

---

<div class="post-metadata">

**Author:** ![Sebastian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebastian/32/98_2.png) [@Sebastian](https://discourse.elm-lang.org/u/Sebastian)\
**Post date:** [October 28, 2024, 10:52pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/6 "2024-10-28T22:52:50Z")

</div>

We used to have helper functions like these to avoid writing the tuples. But it just introduced more cognitive load. So we removed them and just use `(model, cmd)` all the time.

---

<div class="post-metadata">

**Author:** ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)\
**Post date:** [October 28, 2024, 11:39pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/7 "2024-10-28T23:39:16Z")

</div>

> [@rupert](#):
>
> > [@jfmengels](#):
> >
> > A nice benefit of this is that if the model doesn’t change when we use `NoChange`, then we don’t need to update the model either in the “parent” module, which can be beneficial to avoid unnecessary HTML lazy failures.
> 
> Same with your `Effect msg` variant, since that also does not change the model?

Yes, absolutely. I’ve been thinking about this pattern for `elm-review` visitors, with performance in mind (but that’s more of a problem for rules than for `update` calls).

> [@Sebastian](#):
>
> We used to have helper functions like these to avoid writing the tuples. But it just introduced more cognitive load.

Yes, the simplicity and familiarity of the simple tuple is a big selling point.

Also, let’s not forget that not having a dedicated type means you are quite free with the return value: Have an `update` function that only returns `Model`, or return an “out msg” as well, …

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 29, 2024, 8:42am UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/8 "2024-10-29T08:42:37Z")

</div>

> [@jfmengels](#):
>
> Also, let’s not forget that not having a dedicated type means you are quite free with the return value: Have an `update` function that only returns `Model`, or return an “out msg” as well, …

I find it useful to stick to the exact same return pattern everywhere:

updateLikeFn : … → Model → (Model, Cmd Msg)

And then use the update-helper functions to `map`, `andThen` and so on:

```auto
map : (a -> b) -> ( a, Cmd msg ) -> ( b, Cmd msg )
andThen : (model -> ( model2, Cmd msg ))
    -> ( model, Cmd msg )
    -> ( model2, Cmd msg )
pure : model -> ( model, Cmd msg )

```

`pure` is just the no-op case.

```auto
case msg of
    ...

    _ -> 
        pure model

```

The reason I like to stick with the 2-tuple is that it keeps the shape of the code regular, and inside the update case statements you get pipeline style code which is quite readable. I also dislike the out message pattern because of the danger of falling into the actor model message passing way of thinking about Elm modules, which I increasingly think is not a good pattern. Finally, it saves juggling between 2-tuple and 3-tuple or model-only forms which is a bit awkward.

That said, I am pretty sure the `pure`, `map` and `andThen` functions could be re-written to work over this proposed `Update` return type.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 29, 2024, 8:49am UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/9 "2024-10-29T08:49:32Z")

</div>

> [@jfmengels](#):
>
> ```auto
> type Update model msg
> = NoChange
> | NewModel model
> | Effect msg
> | NewModelAndEffect model msg
> 
> ```

BTW, In this type do you mean the `msg` parameter to be an effect? That is, with some code elsewhere that will translate that into a `Cmd`?

The reason I ask is that not every `Cmd msg` can be encoded as its unwrapped `msg`.

```auto
type Msg
    = MyCustomMsg
    | RandInt
    | ...

-- produces Cmd Msg from Msg that simply wraps the Msg as an event
Task.perform identity (Task.succeed MyCustomMsg) 

-- produces Cmd Msg from Msg that performs the specific side effect of generating a random int.
-- no way to represent that particular Cmd Msg as (Effect msg)
Random.generate RandInt Random.int -- produces Cmd Msg

```

Or do we actually mean this?

```auto
type Update model msg
   = NoChange
   | NewModel model
   | Effect (Cmd msg)
   | NewModelAndEffect model (Cmd msg)

```

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [October 29, 2024, 2:45pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/10 "2024-10-29T14:45:17Z")

</div>

I think the thing I like about wrapping the `( model, Cmd msg )` can be best demonstrated with the difference between

```elm
let
    nextModel = { model | foo = someFn model }

    nextNextModel = { nextModel | bar = otherFn nextModel }
in
( { nextNextModel | baz = lastFn nextNextModel }, Cmd.none )

```

vs

```elm
{ model | foo = someFn model }
    |> Update.save
    |> Update.map otherFn
    |> Update.map lastFn

```

The first approach can be very bug prone whereas the second approach feels much more clear (to me) about what the steps are and the order of them.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 29, 2024, 4:55pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/11 "2024-10-29T16:55:31Z")

</div>

> [@wolfadex](#):
>
> ```auto
> { model | foo = someFn model }
> |> Update.save
> |> Update.map otherFn
> |> Update.map lastFn
> 
> ```

Yes, and it can be nice to read code like that too, if you choose good names for your functions. For example I have code like this:

```elm
    ( model, Cmd.none )
        |> andThen (moveCursorRowBy 1 pos)
        |> andThen scrollIfNecessary
        |> andThen calcViewableRegion
        |> andThen rippleBuffer
        |> andThen activity

```

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [October 29, 2024, 4:58pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/12 "2024-10-29T16:58:14Z")

</div>

Discussing this at work and it was pointed out that you can also do

```elm
( model, Cmd.none )
        |> scrollIfNecessary
        |> Tuple.mapFirst activity

scrollIfNecessary : ( model, Cmd msg ) -> ( model, Cmd msg )

activity : model -> model
-- etc

```

---

<div class="post-metadata">

**Author:** ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)\
**Post date:** [October 29, 2024, 6:23pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/13 "2024-10-29T18:23:28Z")

</div>

> [@rupert](#):
>
> Or do we actually mean this?
> 
> ```auto
> type Update model msg
> = NoChange
> | NewModel model
> | Effect (Cmd msg)
> | NewModelAndEffect model (Cmd msg)
> 
> ```

Yes, this is more what I had in mind.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 29, 2024, 7:23pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/14 "2024-10-29T19:23:45Z")

</div>

> [@rupert](#):
>
> ```auto
> type Update model msg
> = NoChange
> | NewModel model
> | Effect (Cmd msg)
> | NewModelAndEffect model (Cmd msg)
> 
> ```

Maybe some advantage in the original though:

```gfd
type Update model e
   = NoChange
   | NewModel model
   | Effect e
   | NewModelAndEffect model e

-- Throughout the application:

update : Model -> Msg -> Update Model (Effect Msg)

-- Then at the effect translation in the top-level

effectToCmd : Update Model (Effect Msg) -> Update Model (Cmd Msg)

```

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 29, 2024, 9:01pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/15 "2024-10-29T21:01:29Z")

</div>

> [@wolfadex](#):
>
> Discussing this at work and it was pointed out that you can also do
> 
> ```auto
> ( model, Cmd.none )
> |> scrollIfNecessary
> |> Tuple.mapFirst activity
> 
> scrollIfNecessary : ( model, Cmd msg ) -> ( model, Cmd msg )
> 
> activity : model -> model
> -- etc
> 
> ```

Sure that works, but as I say, I like to keep all my update-like functions in the same pattern, so:

```auto
scrollIfNecessary : Model -> ( Model, Cmd Msg )
activity : Model -> ( Model, Cmd Msg )
whatever : ... -> Model -> ( Model, Cmd Msg )

```

This is a bit inneficient, since for each andThen I do a Cmd.batch that is just going to be batching Cmd.nones for all the functions that do not create side effects. But in practice, I find the runtime overhead of this to be negligable.

---

<div class="post-metadata">

**Author:** ![Sebastian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebastian/32/98_2.png) [@Sebastian](https://discourse.elm-lang.org/u/Sebastian)\
**Post date:** [October 29, 2024, 9:17pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/16 "2024-10-29T21:17:35Z")

</div>

> [@wolfadex](#):
>
> ```auto
> { model | foo = someFn model }
> |> Update.save
> |> Update.map otherFn
> |> Update.map lastFn
> 
> ```

We do something like this, but more like @rupert said:

```auto
(model, Cmd.none)
    |> Return.andThen doOneThing
    |> Return.andThen doTheOtherThing

```

Each `andThen` can return a new model and batch more commands  
[https://package.elm-lang.org/packages/Fresheyeball/elm-return/latest/Return#andThen](https://package.elm-lang.org/packages/Fresheyeball/elm-return/latest/Return#andThen)

This pattern has been great for us.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 8, 2024, 9:18pm UTC](https://discourse.elm-lang.org/t/thoughts-on-an-update-wrapper/10030/17 "2024-11-08T21:18:11Z")

</div>

Closed on November 8, 2024, 9:18pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
