Time for action – Implementing a parameterized Dynamic

When implementing a parameterized Dynamic, you will get the same behavior as with a non-parameterized Dynamic except that the fields that are not declared in the class will be of the type given as a parameter.

Let's take almost the same example but with a parameterized Dynamic:

class User implements Dynamic<String>
{
   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"; //String because of the type parameter

What just happened?

As you can see here, fields that are not declared in the class are of type String because we gave String as a type parameter.

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.