Skip to content

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 ​

neo
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):

neo
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 !=:

neo
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 a non_exhaustive_case error listing the missing variants.
  • Repeating a variant produces a duplicate_case_arm warning.
  • Including a _ wildcard arm after all variants are already covered produces an unreachable_wildcard warning.