Table of Contents
Casting and type handlingZeoScript 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 int i set i 0 echo <cast string i>
Note that since ZeoScript implements automatic casting, the manual cast of i from an integer to a string is redundant. Casting precision and clippingZeoScript, 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 <cast int64 f> echo i64 // cast to an 8-bit unsigned int byte b set b <cast byte i64> echo b // cast to an 8-bit signed int sbyte c set c <cast sbyte b> echo c // cast back up to an unsigned 32-bit integer uint ui32 set ui32 <cast uint c> 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'. Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
|