layout | title | category | tags | order | ||
---|---|---|---|---|---|---|
developer-doc |
Pattern Matching |
types |
|
5 |
Pattern matching in Enso follows typical operations promoted by various other functional languages. Typing information is always refined in the branches of a case expression.
It is possible to match on the scrutinee by structure for an atom:
from Standard.Base.IO import println
type Vector a
V2 x:a y:a
V3 x:a y:a z:a
main =
v = Vector.V3 "a" "b" "c"
case v of
Vector.V3 x _ _ -> println x
Matching purely by the types involved, and not matching on structure.
case v of
v3:Vector -> print v3.x
[!WARNING] > Unsupported: Name Matching on Labels
Matching on the labels defined within a type for both atoms and typesets, with renaming.
case v of Vector.V3 {x y} -> print x {x} -> print x
Ascribing a name of a scrutinee is done using the standard typing judgement. This works due to the type-term unification present in Enso.
v = Vector.V3 "a" "b" "c"
f = case _ of
Vector.V3 x _ _ -> println x
f v