Parameterized Dynamic class
You can parameterize the Dynamic class to slightly modify its behavior. When parameterized, every field of a Dynamic variable will be of the given type.
Let's see an example:
class DynamicTest { public static function main() { var dynamicVar : Dynamic<String>;dynamicVar = {}; dynamicVar.name = "Benjamin"; //name is a String dynamicVar.age = 12; //Won't compile since age is a String } }
In this example, dynamicVar.name
and dynamicVar.age
are of type String
, therefore, this example will fail to compile on line 7 because we are trying to assign an Int
to a String
.
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.