Enum members
Values can be specified manually. Otherwise, auto-calculated as+1.
Enums are distinct types, not integers
Color.Red is Color, not int, although it holds the value 0 at runtime.
- used as variable and parameters,
- extended with methods for an enum,
- used in struct fields, unions, generics, and other type contexts:
match for enums is exhaustive
Pattern matching on enums requires coverage of all cases:
else to handle remaining values:
== compares values directly:
Enums are integers under the hood
At the TVM level, every enum is represented asint. Casting between the enum and int is allowed:
Color.Blue as intevaluates to22 as Colorevaluates toColor.Blue
as can produce invalid enum values (e.g., 100 as Color). Then operator == will return false, and exhaustive match will throw 5 (integer out of range).
During deserialization with fromCell(), the compiler performs checks to ensure that encoded integers correspond to valid enum values.
Enums in Tolk differ from Rust.
In Rust, each enum member can have a distinct structure.
In Tolk, union types provide that capability, so enums are only integers.
Enums are allowed in throw and assert
Stack layout and serialization
Every enum is backed by TVM INT. Serialized as(u)intN where N is:
- specified manually:
enum Role: int8 { ... } - or calculated automatically to fit all values