Match expressions

Exhaustiveness, guards, and tag-only enums.

let n = match color {
    Color.Red => 1
    Color.Green => 2
    Color.Blue => 3
}

Nested enum binds

Peel a payload enum inside a variant arm. Shorthand infers the inner enum from the payload type:

enum Option_i32 { None, Some(i32) }
enum Result_Opt { Ok(Option_i32), Fail(Option_i32) }

match r {
    Result_Opt.Ok(Some(x)) => x
    Result_Opt.Ok(Option_i32.None) => 0
    Result_Opt.Fail(_) => -1
}

Enum variants with different payload types in one enum (e.g. Ok(Option) + Err(i32)) are not supported yet.

Struct and tuple patterns

destructure structs and tuples in match arms. Field shorthand binds the field name; use _ to ignore a slot.

match p {
    Point { x, y } => x + y
}

match pair {
    (a, b) => a + b
}

Field-value patterns (e.g. Point { x: 0, y }) are not supported yet — patterns bind fields only.

Or-patterns

Combine patterns that share the same body with | (single pipe, not ||):

match c {
    Color.Red | Color.Blue => 1
    Color.Green => 2
}

match method {
    "GET" | "HEAD" => 200
    "POST" | "PUT" => 201
    _ => 400
}

Guards

match r {
    Result.Ok(v) if v > 0 => v
    Result.Ok(_v) => 0
    Result.Err(e) => e
}

Payload enums

Import stdlib/option.ny for generic Option<T> / Result<T,E>, or declare a monomorph enum:

enum Option_i32 { None, Some(i32) }
let x = Option_i32.Some(42)
let n = match x {
    Option_i32.Some(v) => v,
    Option_i32.None => 0,
}

match on a parameter typed Option<T> or Result<T,E> lowers the same as on a local binding. Result.Ok(7) at a call site is monomorphized from the callee parameter type (no extra let annotation needed).

String matching

match works on string scrutinees with string literal arms (compared via strcmp). Use _ for the default arm.

fn http_status(method) {
    return match method {
        "GET" => 200,
        "POST" => 201,
        _ => 404,
    }
}

Arm bodies may use a trailing comma. Built-in tag-only Option/Result (no import) support ?? / ?. only — not stored payloads.