Enums
Enums define a nominal, unparameterized logic type with named variants mapped to explicit integer values. Enums support ==, !=, and exhaustive case expressions. For variants that carry typed payloads, see Unions & Option.
Definition
pub enum AluOp {
Add = 0,
Sub = 1,
And = 2,
Or = 3,
}The bit width of an enum is the minimum number of bits needed to represent its largest variant value, with a minimum width of 1 (so a 1-value enum such as enum Unit { Only = 0 } has width 1). You can query the width at compile time via AluOp.width.
Usage
An enum name evaluates directly to a LogicType, and its variants are accessed with EnumName.Variant (or leading-dot shorthand .Variant when the expected type is known):
module Alu(t: Timing) {
input op(t): AluOp,
input a(t): Int(32),
input b(t): Int(32),
output out(t): Int(32),
} {
comb {
out = case op {
.Add => a + b,
.Sub => a - b,
.And => (a & b).asInt(),
.Or => (a | b).asInt(),
};
}
}You can also compare enum values with == and !=:
logic is_add(t): Bits(1) = op == AluOp.Add;Exhaustiveness Checking
Neo statically verifies that case expressions on enums cover every variant:
- Omitting a variant without a
_wildcard arm produces anon_exhaustive_caseerror listing the missing variants. - Repeating a variant produces a
duplicate_case_armwarning. - Including a
_wildcard arm after all variants are already covered produces anunreachable_wildcardwarning.