6.5. Pattern matching
The MATCH module implements pattern matching on variants, structs, tuples,
arrays, and scalar values. Supports variable capture, wildcards, guard
expressions, and alternation. static_match enforces exhaustive matching
at compile time.
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 {
match (c) {
if (Color.red) { return "red"; }
if (Color.green) { return "green"; }
if (_) { return "other"; }
}
return "?"
}
[export]
def main() {
print("{describe(Color.red)}\n")
print("{describe(Color.green)}\n")
print("{describe(Color.blue)}\n")
}
// output:
// red
// green
// other
6.5.1. Call macros
- match::match
Implements match macro.
- match::static_match
Implements static_match macro.
- match::multi_match
Implements multi_match macro.
- match::static_multi_match
Implements static_multi_match macro.
6.5.2. Structure macros
- match::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.
- match::match_copy
Implements match_copy annotation. This annotation is used to mark that structure can be matched with different type via match_copy machinery.