Elm Language
Pattern Matching
Ricerca…
Argomenti di funzione
type Dog = Dog String
dogName1 dog =
case dog of
Dog name ->
name
dogName2 (Dog name) ->
name
dogName1 e dogName2 sono equivalenti. Si noti che questo funziona solo per ADT che hanno un singolo costruttore.
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"
Qui decostruiamo un record e otteniamo anche un riferimento al record non ricostruito.
Argomento decostruito di tipo singolo
type ProjectIdType = ProjectId String
getProject : ProjectIdType -> Cmd Msg
getProject (ProjectId id) =
Http.get <| "/projects/" ++ id
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow