Pattern Matching
match handles explicit cases and the compiler checks exhaustiveness.
Variants
Match union and enum variants by name.
match-variant.brp
match download:
Running(n): print(n.to_string())
_: print("not running")Literals
Match literal strings, numbers, and booleans directly.
match-literal.brp
match command:
"list": print("listing")
_: print("unknown")Tuples
Tuple patterns bind multiple positions at once.
tuple-match.brp
match (left, right):
(Ok(a), Ok(b)): a + b
_: 0List Patterns
List patterns include [], [x], and [head, ...tail].
list-pattern.brp
match args:
["add", item]: item
[]: "missing"
_: "unknown"Wildcard
_ matches a value without binding it.
wildcard.brp
match result:
Ok(value): value
_: 0Exhaustiveness
A match must cover every possible input shape.
exhaustive.brp
match status:
Queued: "queued"
Running(_): "running"
Done(path): path
Failed(msg): msgExample
commands.brp
pure func route(args: List[String]) -> String:
match args:
["add", item]: "add " + item
["list"]: "list"
[cmd, ..._]: "unknown command: " + cmd
[]: "missing command"
func main(args: List[String]) -> Void:
print(route(["add", "milk"]))
Try It
terminal
blorp run commands.brp