嗯,这是一个很好的问题。事实证明,有一种方法可以做到这一点!
大体上
__arguments__
是闪存目标上的特殊标识符,主要用于访问特殊的局部变量
arguments
. 但它也可以用于方法签名,在这种情况下,它会将输出从
test(args: *)
到
test(...__arguments__)
live on Try Haxe
):
class Test {
static function test(__arguments__:Array<Int>)
{
return 'arguments were: ${__arguments__.join(", ")}';
}
static function main():Void
{
// the haxe typed way
trace(test([1]));
trace(test([1,2]));
trace(test([1,2,3]));
// using varargs within haxe code as well
// actually, just `var testm:Dynamic = test` would have worked, but let's not add more hacks here
var testm = Reflect.makeVarArgs(cast test); // cast needed because Array<Int> != Array<Dynamic>
trace(testm([1]));
trace(testm([1,2]));
trace(testm([1,2,3]));
}
}
static protected function test(...__arguments__) : String {
return "arguments were: " + __arguments__.join(", ");
}