Skip to content

Pattern matching

x := .some(5)
match x {
.some(n) => println("got {n}"),
.none => println("nothing"),
}

Separate arms with comma or newline. Missing separator is a named error and zz fix inserts the comma.

.ok / .err / .some always carry a payload — patterns need an arg. Use _ to ignore: .ok(_). Only .none matches bare.

match .ok(5) {
.ok(n) => n * 2,
.err(e) => 0,
}
x := .some(3)
match x {
.some(n) if n > 0 => println("pos"),
.some(n) => println("non-pos"),
.none => println("nothing"),
}

Guards accept the arm’s bindings + literals + operators only. Calls, strings, field/index access are check-time errors naming the subexpression. Move outer vars and helper calls into the arm body.

For user enums the same shape holds:

enum T { Eof, Num(int) }
t := T.Num(5)
match t {
.Eof => println("eof"),
.Num(n) if n > 0 => println("positive: {n}"),
_ => println("other"),
}

A guarded catch-all (_ if cond) does not count as exhaustive — the guard may fail and fall through.

match 42 {
0 => "zero",
1 => "one",
_ => "other",
}
match .some(.ok(2)) {
.some(.ok(n)) => n,
_ => 0,
}

Arms accept statements (=, :=, return, defer) as if braced:

y := 0
x := .some(5)
match x {
.some(v) => y = v,
.none => y = 0 - 1,
}

break / continue keep expression form so divergence checking is unchanged.

Result / Option as a bare statement warns (value discarded):

import std.fs
path := "f.txt"
data := "hi"
fs.write(path, data) // warning: unused Result
match fs.write(path, data) {
.ok(_) => println("ok"),
.err(e) => println(e),
}
// or: _ := fs.write(path, data) // explicit ignore
// or: fs.write(path, data)? // propagate

Block tails are values, never discards — -> Result bodies never warn. Dead arms after _ / bare binding warn as unreachable.

Next: Control flow, Error catalog.