Time for action – Implementing a non-parameterized Dynamic

When one implements a non-parameterized Dynamic in a class, one will be able to access an infinite number of fields in an instance. All fields that are not declared in the class will be of type Dynamic.

So, for example:

class User implements Dynamic
{
   public var name : String;
   public var age : Int;
   //...
}

//...
var u = new User(); //u is of type User
u.name = "Benjamin"; //String
u.age = 22; //Int
u.functionrole = "Author"; //Dynamic

What just happened?

As you can see, the functionrole field is not declared in the User class, so it is of type Dynamic.

In fact, when you try to access a field that's not declared in the class, a function named resolve will be called and it will get the name ...

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.