Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Enums

Enums define a type with several named variants. Variants may be unit-only or carry payload data. Declare them at program scope or inside an entity.

Unit variants

enum Status {
    Pending,
    Active,
    Closed,
    Cancelled
}

Data variants

enum Action {
    Transfer(U256),
    UpdateConfig(u64, u64),
    Pause
}

Construction and match

let status = Status::Active;
let action = Action::Transfer(1000);

let label = match status {
    Status::Pending => "waiting",
    Status::Active => "live",
    Status::Closed => "done",
    Status::Cancelled => "cancelled"
};

let amount = match action {
    Action::Transfer(amount) => amount,
    Action::UpdateConfig(a, b) => a + b,
    Action::Pause => 0
};

Use _ as a catch-all. Duplicate / unreachable arms after _ are rejected (V47).

Members and where

enum Phase {
    AwaitingDeposit,
    Funded
}

m_phase: Phase {
    in create(_, _) => Phase::AwaitingDeposit
    in fund() => Phase::Funded
}

routes {
    bid(amount: U256)
        where m_phase == Phase::Funded : throw NotFunded()
    => []
}

EVM lowering (tagged unions)

Solidity has no native sum type. Payload-bearing enums lower to a tagged-union pair:

  • an enum <Name>_Tag with one variant per Cambrian variant;
  • a struct <Name> with a tag field plus one storage field per payload slot across all variants (named <lowercase_variant>_<index>).

Unit-only enums can lower more simply; once any variant carries data, the tagged-union layout applies. The validator may emit an informational E08 note when this happens. Prefer small payloads and clear variant names so the generated struct stays readable.