Skip to content

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 contain only named cases — no associated data. They compile directly to a .NET System.Enum:

enum Direction
North
South
East
West
end

Cases are written in PascalCase and accessed via dot notation in snake_case:

dir = Direction.north

Cases can carry explicit integer values. Unassigned cases increment from the previous:

enum Priority
Low # 0
Medium # 1
High # 2
Critical = 10
Blocker # 11
end
enum HttpStatus
Ok = 200
NotFound = 404
InternalServerError = 500
end

The default underlying type is Int. An explicit type follows the enum name:

enum SmallIndex: Byte
A
B
C
end

Supported underlying types: Byte, SByte, Int, UInt, Long and ULong.

@[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 = 4
end

The compiler provides |, &, ^, and ~ operators and an includes? method. Implementation details are deferred.


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)
end
end

Each case is a singleton instance, constructed at program startup:

Planet.earth.mass # Float
Planet.earth.surface_gravity # Float

Complex enums compile to two generated types:

  1. A base enum — PlanetEnum { Mercury, Venus, Earth } — the integer discriminator.
  2. A data class — PlanetData with:
    • 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): PlanetData lookup method

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.


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
end
end
Direction.north.opposite # Direction.south

When the case expression’s type is known, when branches can use .case_name shorthand instead of the fully qualified EnumType.case_name:

case direction
when .north then "heading north"
when .south then "heading south"
else "going sideways"
end

The compiler resolves .north to Direction.north from the type of direction.


Simple enums are standard .NET enums and are fully visible to C#:

enum Color
Red
Green
Blue
end

C# 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.