Repository navigation
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
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use