Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

elm

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?

like image 399
Saurabh kukade Avatar asked May 30 '20 09:05

Saurabh kukade


Video Answer


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.

like image 72
Ananth Avatar answered Sep 19 '22 06:09

Ananth


Read | as 'with'.

{ model 'with' content (set to) = newContent }
like image 45
Rick B Avatar answered Sep 22 '22 06:09

Rick B