Enums

Sum types with optional associated data, accessed via Enum::Variant and destructured in pattern matching.

Summary

Simple enums have no payload; variants are accessed as Color::Red. Variants can carry positional data: Shape::Circle(radius). Enums are matched with match, which can destructure the payload in the same arm. The semantic analyzer checks that match arms cover all variants when no wildcard is present. The internal limit is 256 variants per enum (C implementation constraint).

Canonical

-- simple enum
enum Color { Red, Green, Blue }
let c = Color::Red
println(c)                       -- Color::Red

-- enum with associated data
enum Shape {
    Circle(radius),
    Rect(w, h),
    Triangle(a, b, c)
}

let s = Shape::Circle(5)
let r = Shape::Rect(3, 4)

Pattern Matching on Enums

fn describe(shape) {
    return match shape {
        Shape::Circle(r) => "circle r={r}"
        Shape::Rect(w, h) => "rect {w}x{h}"
        Shape::Triangle(a, b, c) => "triangle {a},{b},{c}"
        _ => "unknown"
    }
}

println(describe(Shape::Circle(5)))    -- circle r=5
println(describe(Shape::Rect(3, 4)))   -- rect 3x4