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