Skip to main content

Patterns

Synopsis

Patterns are a notation for pattern matching used to detect if a value has a certain shape, and then to bind variables to parts of the matched value.

Syntax

For most of the Values, there is a corresponding pattern matching operator. Then there are some "higher-order" matching operators which make complex patterns out of simpler ones. This is the complete list:

PatternSyntax
LiteralBoolean, Integer, Real, Number, String, Location, or DateTime
Regular Expression/<Regular Expression>/
Variable declarationType Var
Multi-variable*Var, *Type Var
VariableVar
List[ Pat₁, Pat₂, ..., Patₙ ]
Set{ Pat₁, Pat₂, ..., Patₙ }
Tuple< Pat₁, Pat₂, ..., Patₙ >
NodeName ( Pat₁, Pat₂, ..., Patₙ )
Descendant/ Pat
LabelledVar : Pat
TypedLabelledType Var : Pat
TypeConstrained[Type] Pat
Concrete(Symbol) Token₁ Token₂ ... Tokenₙ

Description

Patterns are used to dispatch functions and conditional control flow, to extract information from values and to conditionally filter values. The pattern following pattern kinds can be arbitrarily nested, following the above syntax:

All these patterns may be used in:

Each pattern binds variables in a conditional scope:

  • in further patterns to the right of the name which is bound in the same pattern
  • in the body of case statement (either a replacement or a statement body)
  • in the conditions and bodies of <If>, <For>, and <While> control flow statements
  • in the yielding expressions of comprehensions and in furter conditions of the comprehensions

Pitfalls

  • If a pattern does not match, then it may be hard to find out why. A small test case is the best thing to create. Often a default alternative which <Throw>s an exception with the value which is not matched can be used to find out why this is happening.
  • If a variable is bound in the scope of a pattern, then it acts as an == test, so make sure to use fresh variables to avoid such accidental collisions.