Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 of the property accessed. You can then return the value you want. This can be very useful to implement some magic things.