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

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

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