Skip to content
CryoCryo home
LanguageControl and data

10Enums

Cryo enums are algebraic data types. Variants may be unit or carry a payload. The compiler enforces exhaustive matching.

10.1 Unit Enums

type enum Color {
    Red;
    Green;
    Blue;
}

const c: Color = Color::Red;

Variants are accessed through the enum name with ::, so Color::Red and TrafficLight::Red are unambiguous.

Variants may have explicit integer values for FFI or protocol encoding:

type enum ErrorCode {
    Ok       = 0;
    NotFound = 404;
    Internal = 500;
}

10.2 Variants with Payloads

A variant can carry one or more values:

type enum Shape {
    Circle(f64);
    Rectangle(f64, f64);
    Point;
}

const s: Shape = Shape::Circle(5.0);

A Shape value is always exactly one variant; the compiler tracks which one and enforces exhaustive matching.

10.3 Generic Enums

Enums can be parameterised over types. The standard library's Option and Result are the canonical examples:

type enum Option<T> {
    Some(T);
    None;
}

type enum Result<T, E> {
    Ok(T);
    Err(E);
}

const v: Option<int>          = Option::Some(42);
const r: Result<string, int>  = Result::Ok("success");

Each instantiation is an independent type at the machine level; Option<int> and Option<string> share no runtime representation.

10.4 Methods on Enums

Enums cannot declare methods inline. Methods are added via an implement block, which is also how the standard library gives Option and Result their rich API:

implement enum Option<T> {
    is_some(&this) -> boolean {
        match (this) {
            Option::Some(_) => { return true; }
            Option::None    => { return false; }
        }
    }

    unwrap(&this) -> T {
        match (this) {
            Option::Some(value) => { return value; }
            Option::None        => { panic("unwrap on None", FILE, LINE); }
        }
    }
}

See section 13 for the full implement-block grammar.