SplitScript reference / Language / match

match

keyword

match value { pattern => expression }

Exhaustively matches a value.

match supports enum payloads, partial field-based struct patterns, optional None/Some(value) patterns, iterator End/Item(value) patterns, fallible Err(error)/Ok(value) patterns, recursive exact and array rest patterns, string, character, integer, boolean, and file-version literals, closed integer range patterns, guards, a wildcard, and recursive left | right alternatives. A struct pattern ignores omitted fields; Name { field } binds that field, while Name { field: pattern } recursively tests it. Because a match scrutinee supplies a concrete type, the same patterns may usually be shortened to { field } and { field: pattern }; they are still nominal and never select a struct merely by its field names. Alternatives are tried left to right and contribute their union to exhaustiveness. Every alternative in one arm must bind exactly the same names with compatible types; those occurrences form one logical binding for the guard and body. Array elements can bind values or contain any other pattern. A [T; N] exact pattern must have exactly N elements; .. instead permits an omitted middle, including in growable [T] patterns. String patterns compare contents, not WebAssembly GC identities. Enum, wrapper, and array matches are checked recursively for exhaustiveness; guarded arms do not establish coverage.

Examples

Handle every enum variant

let label = match mode {
    Mode.Menu => "Menu",
    Mode.Playing => "Playing"
}

Dispatch on exact string contents

return match name {
    "game.exe" => "full game",
    "game-demo.exe" => "demo",
    _ => "unsupported",
}

Destructure an exact array shape

return match bytes {
    [0x53, value, 0] => value,
    _ => 0,
}

Match selected struct fields

return match point {
    Point { label: "start", x } => x,
    _ => 0,
}