Skip to content

Pattern Matching

Kōdo provides exhaustive pattern matching on enum types using match expressions. The compiler verifies that all variants are handled, preventing bugs from unmatched cases.

enum Direction {
North,
South,
East,
West
}
fn describe(d: Direction) -> String {
match d {
Direction::North => { return "Going north" }
Direction::South => { return "Going south" }
Direction::East => { return "Going east" }
Direction::West => { return "Going west" }
}
}

Enum variants can carry data, which you can destructure in match arms:

enum Shape {
Circle(Int),
Rectangle(Int, Int)
}
fn area(s: Shape) -> Int {
match s {
Shape::Circle(r) => { return r * r * 3 }
Shape::Rectangle(w, h) => { return w * h }
}
}

The compiler requires all variants to be handled. Missing a variant produces a compile-time error, ensuring no unmatched cases at runtime.

Pattern matching is the primary way to work with Option<T> and Result<T, E>:

fn describe_result(r: Result<Int, String>) -> String {
match r {
Result::Ok(v) => { return "success" }
Result::Err(e) => { return "failure" }
}
}

list_get returns the element directly (not wrapped in Option), but many standard library functions like file_read return Result<T, E> that must be matched.

See examples/enums.ko for a complete working example with enum types and pattern matching.