O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Static typing

In haXe, once a variable has been declared and its type is known, it is not possible to assign a value of another type to it. Note however that the compiler allows you to redeclare a variable with the same name and another type. So, the following code compiles:

class Main
{
public static function main()
{
var e : String;
e = "String";
var e : Int;
e = 12;
}
}

However, although this code compiles, you should not be doing this because it might be confusing to other developers or even to you as well, if you have to read your code several days later.

Therefore, to keep things simple, once a variable is typed, its type cannot be changed, and only values of this type can be assigned to it. So, the following wouldn't work:

class Main { public ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required