Prev | Current Page 93 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


To use the Dynamic type, enter the following code into a file called DynamicTest.hx and compile it to
Neko:
class DynamicTest
{
static public function main()
{
var dyn : Dynamic;
dyn = 16;
(continued)
Part I: The Core Language
36
var str : String = dyn;
var int : Int = dyn;
var flt : Float = dyn;
trace( str + ??? ??? + int + ??? ??? + flt );
}
}
If you entered this code correctly, the output should be: 16 16 16 .
As you can probably imagine, had the variable dyn been of the String or Int type, passing the value of
dyn to one of the other variables would have resulted in a compiler error. As it is, even though you
strongly typed your str , int , and flt variables, the compiler noticed that you were only assigning a
value from a Dynamic type and so your typing was removed and the three variables were set to a
null ??” or unknown ??” type, which is no type at all.
In any situation, the compiler understands that the data held in a Dynamic type is not known at design
time, or indeed run time. However, you will not be able to force data of one type into a new form.


Pages:
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105