Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/elfrask/cls/llms.txt

Use this file to discover all available pages before exploring further.

CLS enums are literals with identity — they differ fundamentally from type-alias unions. A alias Color = "red" | "green" is a compile-time annotation that dissolves at runtime; no runtime value ever is a Color. An enum Color { Rojo, Verde, Azul } creates real runtime values where each variant carries its definition name, its variant name, and its numeric index within the enum. Because of this identity, enums support equality checks, membership tests (is), and iteration — making them ideal for state machines, protocol tags, and embedded targets where you want descriptive names that compile down to 1–2 bytes.

Declaration

Declare an enum with the enum keyword, a name, and a comma-separated list of variant names inside braces. A trailing comma after the last variant is allowed.
enum Color {
    Rojo,
    Verde,
    Azul,
};

enum Estado {
    Apagado,
    Encendido,
    EnEspera,
};
Variant names follow the same identifier rules as variables. By convention they use PascalCase.

Accessing variants

Variants are accessed via the enum name as a namespace separator:
var c = Color.Rojo;

print(c);             # → Rojo
print(Color.Verde);   # → Verde
print(Color.Azul);    # → Azul
Printing an enum value displays the variant name as a string — not a number.

Comparison

Use == to compare two enum values. Equality is checked by identity (definition name + variant index), so variants from different enums are never equal even if they have the same name.
var c = Color.Rojo;

print(c == Color.Rojo);   # → true
print(c == Color.Verde);  # → false
print(c == Color.Azul);   # → false

The is operator

The is operator tests whether a value belongs to a specific enum definition. This is useful for runtime type guards, especially when a function can receive values from multiple enum types.
var c = Color.Rojo;

print(c is Color);    # → true
print(c is Estado);   # → false
Use is inside if guards to narrow the type of a variable before switching on its variant.

Iteration

Enums are iterable. for each visits every variant in declaration order:
for each v in (Color) {
    print("variante:", v);
}
# Output:
# variante: Rojo
# variante: Verde
# variante: Azul

Iteration with index

Add and <name> to receive the zero-based variant index alongside each value:
for each v and i in (Estado) {
    print(i, ":", v);
}
# Output:
# 0 : Apagado
# 1 : Encendido
# 2 : EnEspera
The index is the same value that compiles to a u8/u16 in a native binary.

Type annotation

In strict mode the type checker accepts the enum name as a type annotation. Assigning a non-enum value raises a type error.
var color2: Color = Color.Verde;   # ✅ ok
var bad:    Color = 5;             # ❌ error in strict mode: Int is not Color
Type-annotated enum variables combine self-documenting code with static validation — the checker rejects mismatched enums too (var c: Color = Estado.Apagado; is an error).

Enums in switch / case

Because enum variants compare by equality, they work naturally as switch patterns:
var estado = Estado.Encendido;

switch (estado) {
    case (Estado.Apagado)   { print("off");     }
    case (Estado.Encendido) { print("on");      }
    case (Estado.EnEspera)  { print("standby"); }
    default                 { print("unknown"); }
}

Runtime representation

At runtime the interpreter maintains two value kinds for enums:
Value kindContentsPurpose
Value::EnumDefName + ordered list of variant stringsThe enum definition itself — stored as a variable
Value::Enum{ def_name, variant, index }A concrete variant value
The index field is the zero-based position of the variant in the declaration. When targeting a native binary, the index is emitted as a u8 (0–255 variants) or u16 (up to 65 535 variants) — far cheaper than heap-allocated strings.
# These two values are stored as:
#   Value::EnumDef { name: "Color", variants: ["Rojo", "Verde", "Azul"] }
#   Value::Enum    { def_name: "Color", variant: "Rojo", index: 0 }

var c = Color.Rojo;

Exporting enums

Prefix the declaration with export to make the enum available to other modules that import the file:
export enum Color {
    Rojo,
    Verde,
    Azul,
};
From another module:
import "colors" as lib;

var c = lib.Color.Rojo;
print(c is lib.Color);    # → true

Enums vs type-alias unions

Featureenumalias union
Runtime identity✅ real value❌ erased at runtime
Equality (==)✅ by identityString/literal equality only
is membership test
Iteration
Compile target size1–2 bytesvaries (string length)
Type-checker narrowing
Use enum when you need runtime behaviour (switching, iteration, membership checks). Use alias when you only need the type checker to validate that a string or number belongs to a known set.
Enum variants currently carry no associated payload (no data attached to a specific variant). Pattern matching with per-variant data is planned as a future language feature. For now, pair an enum with a record or structure if you need variant-specific data.
The following is drawn directly from examples/tests/test-enums.clsx:
enum Color {
    Rojo,
    Verde,
    Azul,
};

enum Estado {
    Apagado,
    Encendido,
    EnEspera,
};

function main(args: String[]) -> int {
    print("=== acceso ===");
    var c = Color.Rojo;
    print("c:", c);
    print("Color.Verde:", Color.Verde);
    print("Color.Azul:", Color.Azul);

    print("=== comparacion ===");
    print("c == Color.Rojo:",  c == Color.Rojo);
    print("c == Color.Verde:", c == Color.Verde);
    print("c == Color.Azul:",  c == Color.Azul);

    print("=== is ===");
    print("c is Color:",  c is Color);
    print("c is Estado:", c is Estado);

    print("=== tipado estricto ===");
    var color2: Color = Color.Verde;
    print("color2:", color2);

    print("=== iteracion ===");
    for each v in (Color) {
        print("variante:", v);
    };

    print("=== iteracion con indice ===");
    for each v and i in (Estado) {
        print(i, ":", v);
    }

    return 0;
};

Build docs developers (and LLMs) love