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:

(int64)set‑operand

(int32)set‑operand

(int16)set‑operand

(int8)set‑operand

(uint64)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

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 set data type Core::Layout since the destination operand d is a local variable declared with Core::Layout as data type:

var int32        num = 3;
var Core::Layout d;

// Apply an implicit conversion from 'int32' to 'Core::Layout' set
d = num;  // d = Core::Layout[ResizeHorz,ResizeVert]

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.