Prev | Current Page 81 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

This is not the same as dynamically
typing, as the data will still need to be of a consistent type. When the compiler scans your code, it
attempts to ensure that the type assigned to the variable does not deviate throughout its lifetime.
Type inference will have a great impact on your applications when you start using functions. You are
introduced to functions in Chapter 4 , ??? Controlling the Flow of Information. ???
Enter this code into a new file and save it as TypeInference.hx :
class TypeInference
{
public static function main()
{
var myVar = 10.5;
trace(myVar);
}
}
(continued)
Chapter 3: Learning the Basics
31
Now compile and run this example. You should see the content of myVar displayed in the command
window. Now modify the code to include these new lines:
class TypeInference
{
public static function main()
{
var myVar = 10.5;
trace(myVar);
myVar = ???The quick brown fox.???;
trace(myVar);
}
}
When you compile this example, you should be presented with an error that states the following:
TypeInference.


Pages:
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93