Skip to content

Latest commit

 

History

History
74 lines (54 loc) · 1.44 KB

pattern-matching.md

File metadata and controls

74 lines (54 loc) · 1.44 KB
layout title category tags order
developer-doc
Pattern Matching
types
types
pattern-matching
5

Pattern Matching

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.

Positional Matching

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

Type Matching

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

Naming Scrutinees

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