I have to convert a bunch of code from as3 to haxe, but I’m running into a lot of instances of functions using the func( …rest ) syntax.
From my understanding, there’s no way to implement that kind of syntax in haxe.
Macros don’t seem quite right because they are a completely different type of entity - or can they be used to somehow transform the function into a reflect.callMethod() syntax?
You could use macros for this, but it would be easier just to pass an array.
public function func(args:Array<Dynamic>):Void {
for(argument in args) {
//...
}
}
Then:
func(["Argument One", "Argument Two", "etc."]);
Two other ways:
public function func (a1:Dynamic = null, a2:Dynamic = null, a3:Dynamic = null, a4:Dynamic = null, a5:Dynamic = null):Void {
}
This is not truly a rest parameter, but allows up to a few different parameters. This is a simple (but somewhat crude) way of doing it without macros, while still being statically typed.
Another way (but dynamic):
public var func:Dynamic;
private function _func (args:Array<Dynamic>):Void {
}
...
func = Reflect.makeVarArgs (_func);
It’s dynamic, but this method (makeVarArgs
) will create a “rest” parameter function out of an existing function that expects an array. You can then call func (1)
or func (1, 2, 3, 4);
and it will work
I’ll probably use one or the other of these methods, thanks.