Skip to content
Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Enums

An enum declares a finite set of named values with one nominal identity. Variants use Type::Variant, and .index is zero-based in declaration order.

enum Signal { Stop, Wait, Go }
let $signal = Signal::Wait
echo $signal $signal.index
let $action = match $signal
{
    Signal::Stop => { "hold" }
    Signal::Wait => { "prepare" }
    Signal::Go   => { "proceed" }
}
echo $action
Signal::Wait 1
prepare

Enum values are not integers. Use .index when a numeric position is needed. .index is read-only. Values of different enum types remain different even if their variant names or positions coincide.

Enums support methods, exact .type tests, arrays, hash keys, and module exports. All variants of one enum share its type; .type does not identify an individual variant. Compare the value with Signal::Go to identify that case.

Enums are useful for mode settings. For example, $tty_color_mode holds a native TtyColorMode value with TtyMonochrome, TtyBasic, Tty256, and TtyTrueColor variants. See terminal formatting.

The JSON library's enum codecs let a public wire name differ from the language's variant name. Such a codec is a deliberate interface choice; a numeric enum index is not automatically a durable protocol identifier.


Related: Match and type guards · Modules · JSON

Clone this wiki locally