Enums and Match
Lux enums are designed for two different jobs:
- scalar enums give names to stable primitive values;
- table enums model tagged values that carry fields.
match uses the enum shape to produce direct Lua comparisons while still giving
the compiler enough information to report missing cases.
Scalar Enums Are Zero-runtime by Default
A scalar enum with a numeric representation lowers to numbers. No runtime enum table is required unless you explicitly ask for one.
String-backed scalar enums lower to string comparisons.
Use scalar enums when the value needs to cross net messages, files, convars, or plain GLua APIs.
Runtime Scalar Enums
runtime asks Lux to emit a table for reflection or external API compatibility.
The runtime marker can also appear after the representation if that reads
better with surrounding declarations.
Without runtime, scalar enum member names are compile-time names only.
Table Enums
Table enums create tagged tables. Use them when each case has different data.
Matching a table enum narrows the fields available in each branch.
The default table tag key is compiler-owned. Code should construct values through the generated constructors instead of writing tags by hand.
Existing Views
An existing view describes externally-created values without generating
constructors. This is useful for GMod or third-party tables that already carry a
stable tag field.
existing views are type-level knowledge for the compiler; they do not change
the source tables.
Fallback and _
Use _ for a fallback branch when the value space is open or when you
intentionally do not handle every case.
Fallback branches should be used deliberately. When matching a closed enum,
leaving out a case without _ is usually a bug.
match Checking Rules
Lux checks match at compile time when the matched value has an enum
availability. Missing cases are diagnostics unless a fallback branch exists.
For scalar enums, branches compare the underlying scalar value. For table enums, branches compare the tag and then bind fields.
Realms and Enums
Enum declarations have realms like other bindings. Exporting an enum cannot make it available in a wider realm than the enum binding itself.
Shared scalar enums are often the right shape for network protocol constants. Server-only or client-only enums are better for local implementation state.