Time for action – Assigning to Dynamic variables

When you declare a variable as being Dynamic, you will be able to assign any value to it at compile time. So you can actually compile this code:

class DynamicTest
{
   public static function main()
   {
      var dynamicVar : Dynamic;
      dynamicVar = "Hello";
      dynamicVar = 123;
      dynamicVar = {name:"John", lastName : "Doe"};
      dynamicVar = new Array<String>();
   }
}

The compiler won't mind even though you are assigning values with different types to the same variable!

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.