What does pipe `|` operator do in case expression of elm-lang?

Viewed 599

I have this following code snippet in my Elm code:

type alias Model =
  { content : String
  }


update : Msg -> Model -> Model
update msg model =
  case msg of
    Change newContent ->
      { model | content = newContent }

What does { model | content = newContent } do? Does it assign (bind) the value of newContent to model as well as content? Is that why the | operator is placed there?

2 Answers

The pipe is not a part of the case expression. It's record update syntax, as described here: https://elm-lang.org/docs/records#updating-records.

{ model | content = newContent }

assigns the value of newContent to the content field in the model record.

Read | as 'with'.

{ model 'with' content (set to) = newContent }
Related