match
A match expression evaluates a source value against a series of patterns and optional guards, returning the result of the first matching arm. Used for conditional logic and destructuring values.
A match expression evaluates a source value and then selects the first arm
whose pattern (and optional guard) matches.
General form:
result := source? | pattern => expression | pattern, guard => expression | * => fallback.
Notes:
Arms are checked top-to-bottom.
*is a wildcard arm that matches anything.Guards are boolean expressions evaluated after pattern binding.
A wildcard arm is typically included as an exhaustive fallback.
Optional-style matching
Used for optional values, which may potentially be empty and require a fallback:
Patterns must be exhaustive, so a wildcard arm is required to handle the _ case. you can match on any pattern, so you can do something like this:
Matrices
Use array patterns with spread to pull out head/last values.
Tuples
Each arm can have an optional guard, which is a boolean expression that must evaluate to true for the arm to match. Guards are only evaluated after pattern variables are bound, and the first one that matches the pattern and the guard is selected.
Enums
Match arms can destructure enum variants directly, including variants that carry values.
This combines literal variant matches (for example :red(100)) with guarded captures (for example :red(x), x > 100).