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.


Enumeration to integer conversion:









Integer to enumeration conversion:



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

Wherever it is not ambiguous, the Chora compiler applies the necessary type conversion implicitly. This is usually the case when you assign the result of an expression to e.g. a variable or you pass it as parameter in a method invocation. In such cases the Chora compiler can derive from the destination operand the corresponding data type and decide whether or not to convert the expression. For example, the following assignment implies the conversion to enumeration data type Core::Direction since the destination operand d is a local variable declared with Core::Direction as data type:

var int32 num = 1; var Core::Direction d; // Apply an implicit conversion from 'int32' to 'Core::Direction' enum d = num; // d = Core::Direction.TopLeft

If the applied implicit type conversion has potential side effects, the Chora compiler will report a warning giving you a chance to review the affected expression. To avoid the warning you can extend the expression by an additional explicit type conversion operator.