[haXe] getting an array of all arguments of a function

Nicolas Cannasse ncannasse at motion-twin.com
Sat Dec 30 15:22:41 CET 2006


> does that exist in haxe ? Or do we have to use a bunch of optional 
> arguments:
> 
>   function foo(?arg1:Dynamic, ?arg2:Dynamic, ?arg3:Dynamic)
>   {
>     ...
>   }
> 
> 
> And if the later is the case, is there an easy way to iterate through 
> all the args ?

I just added the following function to Reflect :

static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
	#if neko
	return untyped __dollar__varargs(function(a) {
             return f(Array.new1(a,__dollar__asize(a)));
         });
	#else flash9
	return throw "Not implemented";
	#else js
	return function() untyped {
		var a = new Array();
		for( i in 0...arguments.length )
			a.push(arguments[i]);
		return f(a);
	};
	#else flash
	return function() { return f(untyped __arguments__); };
	#end
}

You can use it this way :


class Test {
	
	static function display(a : Array<Dynamic>) {
		trace(a);
		return a.length;
	}

	static function main() {
		var f : Dynamic = Reflect.makeVarArgs(display);
		trace(f(1,2,3));
		trace(f());
		trace(f(1,2,3,4,5,6,7,8,9,10));
	}

}

It works on all platforms except Flash9 (would require additional 
compiler support).

Nicolas




More information about the Haxe mailing list