Prev | Current Page 873 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

The following example uses __setfield()
and __resolve() to create a dynamic getter and setter for every field that is not defined in the class:
class MixedHash implements Dynamic
{
public static function main()
{
var r = new MixedHash();
r.name = ???John???; // __setfield not executed
r.age = 35; // __setfield executed
trace(r.name);
trace(r.age);
// trace(r.inexistant); // throws an error
}
public function new()
{
h = new Hash();
}
(continued)
Chapter 16: haXe Advanced Topics
477
public var name : String;
private var h : Hash < Dynamic > ;
function __setfield(name : String, value : Dynamic)
{
trace(name + ??? ??? + Std.string(value));
h.set(name, value);
}
function __resolve(field : String)
{
return if(h.exists(field))
h.get(field)
else
throw ???unexistent field ??? + field;
}
}
Infos Interface
The haxe.Infos interface, already explained in the RTTI section of this chapter, adds an __rtti static
variable to the class declaration that contains the XML description of the class itself.


Pages:
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885