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

Parameterize an enum

Use a type parameter when the same alternatives should carry different kinds of values. Choose the concrete payload type in brackets. Here, T: IntType accepts any supported sized integer. Omit the constraint to allow other stored types.

enum Choice[T: IntType]:
    Empty
    Value(T)

def main() -> Result[(), IoError]:
    selected = Choice[u8].Value(7)
    match selected:
        case Choice.Empty:
            print("empty")?
        case Choice.Value(value):
            print(value)?
    Ok(())
7

A constructor names the instance, as in Choice[u8].Value(7). A pattern names only the enum, as in case Choice.Value(value): the matched value already fixes T, so a pattern takes no type arguments.