Enums
Enums are named sets of constant values. The enum keyword covers two forms:
simple enums backed by a .NET integer type, and complex enums with associated data.
Simple Enums
Section titled “Simple Enums”Simple enums contain only named cases — no associated data. They compile directly
to a .NET System.Enum:
enum Direction North South East WestendCases are written in PascalCase and accessed via dot notation in snake_case:
dir = Direction.northExplicit Integer Values
Section titled “Explicit Integer Values”Cases can carry explicit integer values. Unassigned cases increment from the previous:
enum Priority Low # 0 Medium # 1 High # 2 Critical = 10 Blocker # 11end
enum HttpStatus Ok = 200 NotFound = 404 InternalServerError = 500endUnderlying Type
Section titled “Underlying Type”The default underlying type is Int. An explicit type follows the enum name:
enum SmallIndex: Byte A B CendSupported underlying types: Byte, SByte, Int, UInt, Long and ULong.
Flags Enums
Section titled “Flags Enums”@[Flags] marks an enum for bitwise combination. By convention, values should be
powers of two:
@[Flags]enum Permission None = 0 Read = 1 Write = 2 Exec = 4endThe compiler provides |, &, ^, and ~ operators and an includes? method.
Implementation details are deferred.
Complex Enums
Section titled “Complex Enums”Complex enums associate data with each case. They are declared the same way as
simple enums but include an initialize definition and per-case constructor
arguments:
enum Planet Mercury(3.303e+23, 2.4397e6) Venus(4.869e+24, 6.0518e6) Earth(5.976e+24, 6.37814e6)
getter mass: Float getter radius: Float
def initialize(@mass: Float, @radius: Float) end
def surface_gravity: Float 6.67300e-11 * @mass / (@radius ** 2) endendEach case is a singleton instance, constructed at program startup:
Planet.earth.mass # FloatPlanet.earth.surface_gravity # FloatCompilation Strategy
Section titled “Compilation Strategy”Complex enums compile to two generated types:
- A base enum —
PlanetEnum { Mercury, Venus, Earth }— the integer discriminator. - A data class —
PlanetDatawith:- Instance fields for the associated data (
mass,radius) - Instance methods (
surface_gravity) - A static
Dictionary<PlanetEnum, PlanetData>mapping each discriminator to its singleton instance - A static
Get(PlanetEnum): PlanetDatalookup method
- Instance fields for the associated data (
Planet.earth compiles to PlanetData.Get(PlanetEnum.Earth).
Using a dictionary means enum values do not need to be contiguous — explicit integer values are supported on complex enums the same as on simple ones.
Methods
Section titled “Methods”Both simple and complex enums can define methods. In simple enums they compile to extension methods in IL; in complex enums they are instance methods on the data class:
enum Direction North South East West
def opposite: Direction case self when .north then Direction.south when .south then Direction.north when .east then Direction.west when .west then Direction.east end endend
Direction.north.opposite # Direction.southcase/when Shorthand
Section titled “case/when Shorthand”When the case expression’s type is known, when branches can use .case_name
shorthand instead of the fully qualified EnumType.case_name:
case directionwhen .north then "heading north"when .south then "heading south"else "going sideways"endThe compiler resolves .north to Direction.north from the type of direction.
.NET Interop
Section titled “.NET Interop”Simple enums are standard .NET enums and are fully visible to C#:
enum Color Red Green BlueendC# sees public enum Color { Red, Green, Blue }.
Complex enums are visible as their generated pair (PlanetEnum and PlanetData).
The enum discriminator is usable directly from C# for interop and serialisation.