Prev | Current Page 99 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

You simply pass the
cast function a value and the type of the value you want to convert it to, and the function will return
the newly cast value. Here is your C++ example rewritten for haXe, using the cast function:
var myFlt : Float = cast(44, Float);
Now, of course, you could rely on type inference to pass the literal of 44 to the variable, but under certain
circumstances, this would be the most simple and primary means for conversion. Now, the cast
function worked great for converting an integer to a floating point value, but converting a floating point
value back into an integer is not possible using cast . Why? Well, unfortunately, casting has quite a large
limitation. The variable you pass to the cast function must extend the type that is also passed to the
cast function. If you remember back to when you learned about how haXe incorporates integers and
floats, it is noted how the Int type extends the Float type. This meant that, while an integer is a type of
float, a float is not a type of integer, so the conversion can only flow in one direction.


Pages:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111