Elm Language

Pattern Matching

Function arguments

type Dog = Dog String

dogName1 dog =
    case dog of
      Dog name ->
         name

dogName2 (Dog name) ->
     name

dogName1 and dogName2 are equivalent. Note that this only works for ADTs that have a single constructor.

type alias Pet =
   { name: String
   , weight: Float
   
   }

render : Pet -> String
render ({name, weight} as pet) =
    (findPetEmoji pet) ++ " " ++ name ++ " weighs " ++ (toString weight)

findPetEmoji : Pet -> String
findPetEmoji pet = 
    Debug.crash "Implementation TBD"

Here we deconstruct a record and also get a reference to the undeconstructed record.

Single type deconstructed argument

type ProjectIdType = ProjectId String

getProject : ProjectIdType -> Cmd Msg
getProject (ProjectId id) =
    Http.get <| "/projects/" ++ id

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow