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 code = match status {
Status::Pending => 1,
Status::Active => 2,
_ => 0
};
let amount = match action {
Action::Transfer(amount) => amount,
Action::UpdateConfig(a, b) => (a + b) as U256,
_ => 0
};
Use _ as a catch-all. A match must be exhaustive: either list every
variant or end it with a _ => arm; a match that misses a variant is
rejected. A _ after all variants are already covered is unreachable and
draws a warning, as do duplicate arms and arms after _ (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>_Tagwith one variant per Cambrian variant; - a struct
<Name>with atagfield 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.