To get around type casting restrictions using cast , you can opt for a dynamic route, so that no compiler
checking is performed. To do this, you simply omit the type parameter for the cast function:
var myFlt : Float = cast(44);
This is known as an unsafe cast, and performs the same functionality as passing the data from a Dynamic
type variable as opposed to returning it from a call to the cast function, such as:
var tmp : Dynamic = 44;
var myFlt : Float = tmp;
Chapter 3: Learning the Basics
39
As you saw earlier, this has the affect of shoehorning the value into the variable, but it still doesn ??™ t
actually convert the value as such.
Casting an Int to a Float is a bad example, as for this pair, the haXe compiler will perform this automatically.
However, short of creating your own classes, this would be hard to demonstrate otherwise.
Just keep in mind that the cast function will normally be needed.
Simple Type Conversion Functions
Thankfully, haXe provides yet more functions with the specific purpose of converting simple types from
one type to another; these are found in the Std (pronounced standard ) class.
Pages:
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112