Enums
Enums in Type-C serve as a way to define a set of named constants. These constants are internally mapped to integer. The interal type can change, to allow ease of conversions.
Basic Usage:
In this example, the enum Color defines three identifiers: Red, Green, and Blue. These are internally mapped to integer values, starting from zero, unless explicitly stated otherwise. Thus, in this case, the internal mapping would be Red=0, Green=1, Blue=2.
Custom Values
Type-C also allows for explicit value assignments within enums. These values can be signed or unsigned integers and are not restricted to being sequential. The compiler calculates the size of the integer type based on the minimum and maximum values provided, optimizing storage requirements.
In the HttpStatus example, the enums are explicitly associated with specific HTTP status codes. This results in an enum type with an integer size calculated based on the range 200 to 500.
Notice that if an enum field has a value, the following rules are applied:
- If only the first field has a value, the rest of the fields are assigned values starting from the base value +1.
- If any other enum other than the first have value, than all the fields must have values. Otherwise the compiler will throw an error.
Custom Types
Enums as treated as unsigned number by default. When an enum has value assigned, the compiler will calculate the minimum and maximum values and assign the smallest possible type. This can be overridden by explicitly specifying the type.