Reflect

class ReflectAvailable in flash8, flash, neko, js, php, cpp, cs, javaThe Reflect API is a way to manipulate values dynamically through an abstract interface in an untyped manner. Use with care. static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : DynamicCall a method with the given object and arguments. For example, to call the "foo" method with no arguments on the object "obj" use: Reflect.callMethod(obj, Reflect.field(obj, "foo"), []). static function compare<T>( a : T, b : T ) : IntGeneric comparison function, does not work for methods, see compareMethods. Can only be used with objects that implement __compare(other):Int method. Returns null if the parameters cannot be compared. static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : BoolCompare two methods closures. Returns true if it's the same method of the same instance. static function copy<T>( o : T ) : TAvailable in cs, javaMake a copy of the fields of an object. Works with instances on neko. Only guaranteed to work with anonymous objects on other targets. static function copy<T>( o : T ) : TAvailable in flash8, flash, neko, js, php, cppMake a copy of the fields of an object. Works with instances on neko. Only guaranteed to work with anonymous objects on other targets. static function deleteField( o : Dynamic, field : String ) : BoolAvailable in cs, javaDelete an object field. static function deleteField( o : Dynamic, field : String ) : BoolAvailable in flash8, flash, neko, js, php, cppDelete an object field. static function field( o : Dynamic, field : String ) : DynamicAvailable in cs, javaReturns the field of an object, or null if o is not an object or doesn't have this field. static function field( o : Dynamic, field : String ) : DynamicAvailable in flash8, flash, neko, js, php, cppReturns the field of an object, or null if o is not an object or doesn't have this field. static function fields( o : Dynamic ) : Array<String>Available in cs, javaReturns the list of field names of an object, excluding its methods. Note that this method is only guaranteed to work for anonymous objects. For class/instances fields see Type.getClassFields and Type.getInstanceFields methods. static function fields( o : Dynamic ) : Array<String>Available in flash8, flash, neko, js, php, cppReturns the list of field names of an object, excluding its methods. Note that this method is only guaranteed to work for anonymous objects. For class/instances fields see Type.getClassFields and Type.getInstanceFields methods. static function getProperty( o : Dynamic, field : String ) : DynamicAvailable in cs, javaSimilar to field but also supports property (might be slower). static function getProperty( o : Dynamic, field : String ) : DynamicAvailable in flash8, flash, neko, js, php, cppSimilar to field but also supports property (might be slower). static function hasField( o : Dynamic, field : String ) : BoolAvailable in cs, javaTells if an object has a field set. This doesn't take into account the object's methods. static function hasField( o : Dynamic, field : String ) : BoolAvailable in flash8, flash, neko, js, php, cppTells if an object has a field set. This doesn't take into account the object's methods. static function isFunction( f : Dynamic ) : BoolAvailable in cs, javaTells if a value is a function or not. static function isFunction( f : Dynamic ) : BoolAvailable in flash8, flash, neko, js, php, cppTells if a value is a function or not. static function isObject( v : Dynamic ) : BoolTells if a value is an object or not. static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : DynamicReturns a function equivalent to the given one but that takes any number of arguments instead of an array of arguments. static function setField( o : Dynamic, field : String, value : Dynamic ) : VoidAvailable in cs, javaSet an object field value. static function setField( o : Dynamic, field : String, value : Dynamic ) : VoidAvailable in flash8, flash, neko, js, php, cppSet an object field value. static function setProperty( o : Dynamic, field : String, value : Dynamic ) : VoidAvailable in cs, javaSimilar to setField but also supports property (might be slower). static function setProperty( o : Dynamic, field : String, value : Dynamic ) : VoidAvailable in flash8, flash, neko, js, php, cppSimilar to setField but also supports property (might be slower).
version #19313, modified 2013-05-08 11:13:38 by api
2 comments
  • Sep 20, 2012 at 04:13

    There was a cool hack posted on Stack Overflow for using Reflect.callMethod() to change the "this" scope in a Haxe object. It's completely type un-safe, no compiler time checks etc, but it's still cool:

    // Call `myObject.someMethod()` but change "this" in the function to refer to some object `newThis`
    Reflect.callMethod(newThis, Reflect.field(myObject, "someMethod"), []);
  • amn
    Jun 24, 2013 at 17:36

    Jason, that's not a hack, it's perfectly valid and legal.