====== Casting and type handling ======
ZeoScript is technically a strongly typed language, similar to the type system in the more familiar C programming language. However, for convenience, ZeoScript implements automatic type casting for most common types. Thus, it is usually unnecessary for coders to manually perform type casting.
===== Manual type casting =====
Type casting can be performed manually using the ''cast'' function operator. The following shows how to cast from an integer to a string (like printf) using ''cast'':
int i
set i 0
echo
* In the first line, a new integer called //i// is created.
* In second line, //i// is set to 0.
* In the third line, //i// is cast to a string, and this is written to the event log using the 'echo' function.
Note that since ZeoScript implements automatic casting, the manual cast of //i// from an integer to a string is redundant.
===== Casting precision and clipping =====
ZeoScript, like the C programming language, does not prevent range-clipping when casting to a data type with a reduced range, such as casting from a 64-bit to a 32-bit integer. Such unsafe casting may result in unexpected values.
The effect or range clipping is demonstrated in the example below, where a float is cast to a 64-bit integer, then to an 8-bit unsigned integer, then to an 8-bit signed integer, and finally to an unsigned 32-bit integer. The output is given after the jump...
// create a float, and set to 500.1
float f
set f 500.1
echo f
// cast to 64-bit integer
int64 i64
set i64
echo i64
// cast to an 8-bit unsigned int
byte b
set b
echo b
// cast to an 8-bit signed int
sbyte c
set c
echo c
// cast back up to an unsigned 32-bit integer
uint ui32
set ui32
echo ui32
As expected, the output values of the above script (shown below) are different each type we reduce the precision (e.g. int64->byte), and each time we change from signed/unsigned and vice-versa (e.g. byte->sbyte, sbyte->uint).
500.1 // as a float
500 // as an in64
244 // as a byte (unsigned 8-bit)
-12 // as a sbyte (signed 8-bit)
4294967284 // as an uint (unsigned 32-bit)
The lesson is, be mindful of the effect of your casting when reducing precision or changing 'signedness'.