Type conversion: Any operand to boolean

The programming language Chora supports type conversion between all data types as source and the boolean data type bool as destination.

Syntax

(bool)any‑operandorbool(any‑operand)

Discussion

When converting an operand to bool the operation considers source values equal 0 (zero) or being empty as boolean false while all non-zero or non-empty values are considered as true values. The following table describes the expected conversion results for each existing data type:

Data type

Results of boolean conversion

char

Results in false if the character is '\x0000'. Otherwise the conversion results in true.

class

Results in false if the class is null. Otherwise the conversion results in true.

color

Results in false if the color is #00000000 (fully transparent). Otherwise the conversion results in true.

float

Results in false if the floating-point value is 0.0. Otherwise the conversion results in true.

handle

Results in false if the handle is null. Otherwise the conversion results in true.

int8 int16 int32 int64

Results in false if the integer is 0. Otherwise the conversion results in true.

language

Results in false if the language is Default. Otherwise the conversion results in true.

object

Results in false if the object is null. Otherwise the conversion results in true.

point

Results in false if the point is <0,0>. Otherwise the conversion results in true.

rect

Results in false if the rect is <0,0,0,0>. Otherwise the conversion results in true.

slot

Results in false if the slot reference is null. Otherwise the conversion results in true.

string

Results in false if the string is "" (empty). Otherwise the conversion results in true.

styles

Results in false if the styles set is [] (empty). Otherwise the conversion results in true.

uint8 uint16 uint32 uint64

Results in false if the integer is 0. Otherwise the conversion results in true.

Enumeration type

Results in false if the value does correspond to the first enumeration item or to the item with the explicitly specified value 0. Otherwise the conversion results in true.

Set type

Results in false if the set is [] (empty). Otherwise the conversion results in true.

Property reference

Results in false if the property reference is null. Otherwise the conversion results in true.

Explicit conversion

With the explicit conversion operator you specify explicitly how an operand should be treated within the implemented expression. Chora supports two equivalent notation variants how you can apply the conversion operators. In the first variant the destination data type is specified between a pair of (...) (parentheses). The second notation variant corresponds to a constructor invocation of the respective destination data type:

var int32 a = 30; var int32 c = 12; // Notation 1 var bool b1 = (bool)( a + c ); // b1 = true // Notation 2 var bool b2 = bool( a + c ); // b2 = true

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 or in a Chora statement. 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.

In case of the to boolean conversion the Chora compiler accepts all data types as source and performs the respective conversion according to the rules described in the section above. For example, in order to test whether an integer number is not 0, a variable does refer to an existing object and a string is not empty, it is not necessary to explicitly apply the respective equality operators. Instead the operands can be combined directly in the condition as shown below and the Chora compiler converts each operand to bool:

var int32 number = ... var object obj = ... var string str = ... if ( number && obj && str ) ... // The same operation using equality operators to test the operands if (( number != 0 ) && ( obj != null ) && ( str != "" )) ...