Programming language Chora: Type conversion

Type conversion operators are used whenever an operand in an expression must be subjected to a conversion of its data type. Such a type conversion can be necessary when the data type of the operand can’t be used directly in the expression, or when ambiguities exist in the possible evaluation of the expression.

Depending on the data type of the operands involved in an operation, the operation may produce ambiguous results. In such case the Chora compiler will report a warning or even an error message. Compared with other programming languages, Chora handles such situations very strictly expecting you to express your intention precisely. For example, the addition of an int32 and float operand can result in an int32 or float value. Which of them is then right?

Explicit type conversion

To express your intention you apply in such case an explicit type conversion on one or on both of the operands instructing so Chora to convert the operands just before operating on them. In this manner you can decide whether the addition of an int32 operand with a float operand should be performed with integer or floating-point precision. Usually, to convert an operand in another data type you prefix the operand by the desired data type enclosed between a pair of (...) (parentheses):

var float a = 13.69;
var int32 b = 1251;

// Convert the operand 'b' in 'float' and perform a floating-point
var float c = a + (float)b; // 1264.69

// Convert the operand 'a' in 'int32' and perform an integer addition
var int32 d = (int32)a + b; // 1264

The following table provides an overview of the type conversion operators implemented in the programming language Chora:

Type conversion

Description

Integer to integer

Converting between diverse signed or unsigned integer data types.

Floating-point and integer

Converting between floating-point and signed or unsigned integer data types.

Enum data types and integer

Converting between an enumeration data type and signed or unsigned integer data types.

Set data types and integer

Converting between a set data type and signed or unsigned integer data types.

Char and integer

Converting between char and signed or unsigned integer data types.

Number to string

Formatting a string from a floating-point, signed or unsigned integer data types.

String to number

Parsing a string as number and converting its content in a floating-point, signed or unsigned integer data types.

Char to string

Formatting a string from a char operand.

Any operand to boolean

Converting any data type to bool data type.

Object runtime cast

Evaluating the class ancestry of an object.

Class runtime cast

Evaluating the class ancestry of a class.

Implicit type 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 uint8 data type since the destination operand is the red instant property declared with uint8 as data type:

var int32 a   = 167;
var color clr = #00000000;

// Apply an implicit conversion from 'int32' to 'uint8'
clr.red = a;  // clr.red = 167

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. For example: