Enum members
Values can be specified manually. Otherwise, they are auto-calculated as+1.
Enum types
Enums are distinct types, not integers.Color.Red has type Color, not int, although it holds the value 0 at runtime.
- used as variables and parameters,
- extended with methods,
- used in struct fields, unions, generics, and other type contexts.
Exhaustive pattern matching
match on enums requires coverage of all cases:
else to handle remaining values:
== compares values directly:
Integer representation
At the TVM level, every enum is represented asint. Casting between the enum and int is allowed:
Color.Blue as intevaluates to2;2 as Colorevaluates toColor.Blue.
Operator
as can produce invalid values, for example 100 as Color. In this case, operator == returns false, and an exhaustive match throws exception 5.fromCell(), the compiler validates that encoded integers correspond to valid enum values.
Usage in throw and assert
Enums are allowed in throw and assert:
Stack layout and serialization
Every enum is backed by TVMINT and serialized as (u)intN, where N is:
- specified manually, for example:
enum Role: int8 { ... }; - or calculated automatically to fit all values.