Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the | symbol read in Elm?

Tags:

syntax

elm

Consider the following example code:

-- Update the fields of a record. (It must have the fields already.)
{ person |
  name = "George" }

-- Update multiple fields at once, using the current values.
{ particle |
  position = particle.position + particle.velocity,
  velocity = particle.velocity + particle.acceleration }

Source: Learn Elm in X Minutes

How is one supposed to read | in this example, and in Elm generally?

I'm familiar with it in set-builder notation as "where" / "such that", and in list comprehensions in Haskell it has a very similar purpose, e.g.

[ x*2 | x <- [1..10] ]

is logically equivalent to

enter image description here

source: Learn You A Haskell

(Obviously I'm also familiar with its use as the unary "or" operator in C-like languages)

What about something like type Msg = Increment | Decrement ?

Source: https://guide.elm-lang.org

Or, in this example when discussing Union Types:

type Boolean
    = T
    | F
    | Not Boolean
    | And Boolean Boolean
    | Or Boolean Boolean
like image 445
Benjamin R Avatar asked Jan 04 '23 10:01

Benjamin R


1 Answers

In types I read it as 'or'. In the counter example:

type Msg = Increment | Decrement

I would read it as "a Msg is Increment or Decrement". In a slightly more complex but still common example of the Result type:

type Result error value
    = Ok value
    | Err error

I would read "a Result is either Ok with a value or Err with an error".

In the example you give of the record update syntax, I would read it as 'with' rather than 'where'. For example:

{ person | name = "George" }

is "the person value with its name field set to "George"" (rather than "where the name = 'George'" which seems to imply that you're filtering based on what values are in person). This one is I think more ambiguous than the type case though.

like image 156
robertjlooby Avatar answered Mar 29 '23 05:03

robertjlooby