Patterns (ref)
From Nemerle Homepage
Contents |
Patterns
Patterns are a form of accessing data structures, especially trees. Patterns can match values. A definition of the term to match is given with each pattern construct. However, the main idea behind patterns is that they match values that look like them.
Pattern are used in match expression and value definitions.
Constructor pattern
The identifier should refer to the name of variant option. This pattern
matches a value if it is a specified variant option, and sub-pattern
matches variant option payload.
Throw-away pattern
This pattern matches any value.
Record pattern
This pattern matches a value of a class, that has all specified fields
(this is checked statically), and a value of each field matches respective
pattern. This pattern currently cannot be used to match a value of a variant.
As binding
This pattern matches the same value as an enclosed pattern does. However,
in addition the value matched by the enclosed pattern is bound to a
specified variable, which can be used in when guard or match body.
Type check pattern
This pattern matches a value if it possesses the given type. In addition,
the value matched is bound to a specified variable, which gets the
given type.
This pattern can be used both for checking the type and hinting the type checker (if the value is statically known to always have given type, compiler issues a warning and no runtime checks are performed).
Tuple pattern
This pattern matches a tuple with specified contents (each tuple member
is matched be respective pattern).
In addition, when a tuple pattern is seen, where a record pattern would be otherwise expected -- the tuple pattern is transformed to record pattern by adding field identifiers in order they appear in the definition of the given class. A tuple pattern transformed to a record pattern cannot match fields inherited from the base class.
List constructor pattern
Equivalent to:
list.Cons (pattern, pattern)
List literal pattern
Equivalent to:
list.Cons (pattern1, list.Cons (pattern2, ... list.Cons (pattern2, list.Nil) ... ))
Literal pattern
This pattern matches a specified constant value.
Type enforcement in pattern
This construct is used to statically enforce type of subpattern to given
type. If a matched expression might not have such a type, compiler
issues an error. It is used mainly for hinting type inference engine
and improving code clarity.