Type conversion: Set data types and integer

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

Syntax

Set to integer conversion:

(int32)set‑operand

(int16)set‑operand

(int8)set‑operand

(uint32)set‑operand

(uint16)set‑operand

(uint8)set‑operand

Integer to set conversion:

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

Discussion

The set to integer conversion operator converts the given set operand to a signed or unsigned integer data type. In practice, the conversion is performed in three steps. First the integer values associated with all enumerators active in the set operand are determined. Then a common value is built by adding the individual values. Finally the integer conversion operation is applied on this value in order to convert it to the destination data type.

Depending on the associated enumerator values, 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 set conversion operator converts the given unsigned integer value in an operand of the specified set data type. In practice, this conversion evaluates the enumerators existing within the set and looks for those whose associated values do match the given integer value. During prototyping this operation is strictly supervised in order to avoid that numbers not having an enumerator counterpart within the set do pass the conversion. If such case is detected, a runtime error is reported:

Please note, in the case, the original operand is an empty set, the conversion returns 0 (zero). Similarly, the conversion of the integer value 0 (zero) to a set will result in an empty set.

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 a set the data type is composed of the unit name, the corresponding set member is defined inside, and the name of the set member itself, both separated by :: (double colon) signs. The following example converts the Core::ViewState set operand a1 to uint32 and vice versa:

var Core::ViewState a1 = Core::ViewState[ Visible, Enabled ]; // Convert 'Core::ViewState' set to 'uint32' var uint32 b1 = (uint32)a1; // Convert 'uint32' to the 'Core::ViewState' set var Core::ViewState a2 = (Core::ViewState)b1; trace a1 == a2; // true

The explicit set 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 set conversion evaluates the given integer operand with the aim to find all corresponding enumerators. 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 set 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 set and integer data type within an expression.