Prev | Current Page 259 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

empty() and not {} . The second
statement is in haXe the declaration for an empty block of code that returns Void and not an empty object.
Implementing Dynamic
The use of the Dynamic type is discussed in Chapter 3 ; here you explore how Dynamic can add yet more
flexibility to standard classes.
First of all any newly defined class can implement Dynamic. When this is the case, the instance class fields
are strongly typed if defined in the class. If not, they are of the Dynamic type. Consider the following
example:
class Main
{
static function main()
{
var item = new Item();
// regular access to class field
item.name = ???My Item???;
// now exists a description field with the specified value
item.description = ???Item description???;
// would fail because the type of price is defined in the class and
// must be a numeric value not a string
//item.price = ???-???;
// trace to null because a dynamic object contains every possible field
trace(item.quantity);
}
}
class Item implements Dynamic
{
public var name : String;
public var price : Float;
public function new() {}
}
Dynamic Type Parameters
Dynamic can also take one type parameter; in that case every field of the Dynamic object will have the
type of the passed parameter.


Pages:
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271