6.6. Pattern matching
The MATCH module implements pattern matching on variants, structs, tuples,
arrays, and scalar values. Each arm is pattern => body. A bare name in a
pattern binds the matched value, _ matches anything, && adds a guard,
| separates alternatives, and & requires both patterns. match (a, b)
matches several values at once.
As a statement, a multi-statement body is a $ { } block whose return
leaves the enclosing function. In value position match is an expression:
every body is an expression and the arms must cover every value. An
unreachable arm is a compile error, and so is a match over an enum, bool,
variant or struct pointer that misses a value.
Arms are tried in source order and the first one that matches wins; a pattern
that cannot apply to the subject type is a compile error. static_match
drops such arms silently instead of erroring, which is what makes it usable in
generic code where only some arms apply per instantiation.
See Pattern Matching for a hands-on tutorial.
All functions and symbols are in “match” module, use require to get access to it.
require daslib/match
Example:
require daslib/match
enum Color {
red
green
blue
}
def describe(c : Color) : string {
return match (c) {
Color.red => "red"
Color.green => "green"
_ => "other"
}
}
[export]
def main() {
print("{describe(Color.red)}\n")
print("{describe(Color.green)}\n")
print("{describe(Color.blue)}\n")
}
// output:
// red
// green
// other
6.6.1. Call macros
- match
Implements match macro.
- static_match
Implements static_match macro.
6.6.2. Structure macros
- match_copy
Implements match_copy annotation. This annotation is used to mark that structure can be matched with different type via match_copy machinery.
- match_as_is
Implements match_as_is annotation. This annotation is used to mark that structure can be matched with different type via is and as machinery.