Type conversion: Enum data types and integer

With the enum member you add to the project a user-defined enumeration data type consisting of uniquely named enumerators with automatically associated integer numbers. Since Chora treats the enumerations as individual data types, special type conversion operators are available permitting you to safely convert between the integer and the respective enumeration.

Syntax

Enumeration to integer conversion:

(int32)enum‑operand

(int16)enum‑operand

(int8)enum‑operand

(uint32)enum‑operand

(uint16)enum‑operand

(uint8)enum‑operand

Integer to enumeration conversion:

(unit‑name::enum‑name)integer‑operand

Discussion

The enumeration to integer conversion operator converts the given enum operand to a signed or unsigned integer data type. In practice, the conversion is performed in two steps. First the integer value associated with the enumerator in the enum operand is determined. Then the integer conversion operation is applied on this value in order to convert it to the final destination data type.

Depending on the associated enumerator value, the conversion may lead to the loss of information. Precisely, the conversion of an integer value to a data type with lower rank results in the original value highest bits being truncated. In simple terms, if the value associated with the enumerator doesn't fit in the destination data type, the value resulting from the conversion will differ from the expected value.

The integer to enumeration conversion operator converts the given unsigned integer value in an operand of the specified enumeration data type. In practice, this conversion evaluates the enumerators existing within the enumeration and looks for the one, which matches the given integer value. During prototyping this operation is strictly supervised in order to avoid that numbers not having an enumerator counterpart within the enumeration do pass the conversion. If such case is detected, a runtime error is reported:

Explicit conversion

With the explicit conversion operator you specify explicitly how an operand should be treated within the implemented expression. The desired destination data type is specified between a pair of (...) (parentheses). In case of an enumeration the data type is composed of the unit name, the corresponding enum member is defined inside, and the name of the enum member itself, both separated by :: (double colon) signs. The following example converts the Core::Direction enumeration operand a1 to uint32 and vice versa:

var Core::Direction a1 = Core::Direction.Right; // Convert 'Core::Direction' enum to 'uint32' var uint32 b1 = (uint32)a1; // Convert 'uint32' to the 'Core::Direction' enum var Core::Direction a2 = (Core::Direction)b1; trace a1 == a2; // true

The explicit enumeration to integer conversion is performed with the above described side effects. Applying the conversion on an operand not fitting the destination data type will thus result in the original operand value being truncated. Since you apply the operation consciously, the Chora compiler will not report any warning nor error in such case.

In turn, the integer to enumeration conversion evaluates the given integer operand with the aim to find the corresponding enumerator. If there is no matching enumerator, the conversion fails with a runtime error as described above.

Implicit conversion

The Chora compiler doesn't perform any implicit type conversion between enumeration and integer data types even if the conversion is obvious and unambiguous. In any case you have to specify explicitly the desired conversion operation when mixing operands of enumeration and integer data type within an expression.