Skip to content

Enums

An enum defines a type with a fixed set of variants:

enum State {
Pending,
Running,
Finished,
}
var state = State.Pending;

Variants are accessed through the enum type.

if state == State.Pending {
state = State.Running;
}
if state != State.Finished {
println("work remains");
}

Variants may specify integer values:

enum ExitCode {
Success = 0,
InvalidInput = 2,
Unavailable = 69,
}

Convert an enum value explicitly when an integer is required:

var code: Int = ExitCode.InvalidInput as Int;

Use @repr("C") for an enum passed through a C ABI:

@repr("C")
enum Direction {
Left,
Right,
}

Match the values and representation expected by the C declaration.

Enum variants can also carry values. That separate feature is covered in Tagged unions. Error enums are covered in Error handling.