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 ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.