Skip to content

Enums

Enum is a special class that has a predefined and fixed set of instances, called enum constants.

The following example declares an enum to represent priority:

enum Priority {
LOW,
MEDIUM,
HIGH,
}

All instances of enum are predefined. Trying to explicitly initiate enum leads to compilation error.

Just like class, enum supports constructor, fields and methods.

enum Priority(
val level: int
) {
LOW(1),
MEDIUM(2),
HIGH(3),
;
fn isHigherThan(that: Priority) -> bool {
return level > that.level
}
}

Note that the semicolon separating enum constants from other other members is mandatory.