2

OK, so we all know that using the ... (rest) parameter in a function lets us pass any number of arguments, as in the example in the Adobe AS3 manual:

function traceArgArray(x: int, ... args) {

for (var i:uint = 0; i < args.length; i++) {

trace(args[i]);

}

} 

traceArgArray(1, 2, 3);

BUT, what I would love to be able to do is to have the option of EITHER passing individual arguments OR passing arguments from an already-existing array:

myArray:Array = new Array(1,2,3);

and so:

traceArgArray(myArray);

As I've written it here, the function treats myArray as a single object, so the output is 1,2,3--a representation of the array as a whole, not the individual contents of the array.

In order to transform myArray into a comma-delimited list that the rest operator would recognize as a list of individual elements, I tried this:

traceArgArray(myArray.join());

but that didn't change the output. It looks like in this case the argument is being interpreted as a single string rather than a comma-delimited list.

So, the puzzle I hope someone can help me with can be expressed in either of two ways:

Is it possible to get the rest parameter to treat an array argument as a comma-delimited list of arguments?

-or-

Is there a way I can transform an array into a comma-delimited list and then pass that list to the function AS a comma-delimited list (rather than a long string) so that the rest operator can interpret it correctly?

The basic thing I want to do--be able to pass arrays of values into functions that accept any number of arguments--would seem to be of general use, so perhaps there is a work-around I'm not seeing because I'm obsessing over the rest operator itself.

Thanks for reading!

Chris McCauley
  • 25,824
  • 8
  • 48
  • 65
Dr. Dave
  • 21
  • 2
  • possible duplicate of [Pass arguments from an array to an Actionscript method with ...(rest) argument](http://stackoverflow.com/questions/5260875/pass-arguments-from-an-array-to-an-actionscript-method-with-rest-argument) – Ranhiru Jude Cooray Jul 09 '11 at 18:58
  • possible duplicate of http://stackoverflow.com/questions/636853/filling-in-rest-parameters-with-an-array – Ranhiru Jude Cooray Jul 09 '11 at 19:02

3 Answers3

2

What you're looking for is Function.apply(). In your case it would look like this:

myArray:Array = new Array(1,2,3);

function traceArgArray(x: int, ... args) {
    for (var i:uint = 0; i < args.length; i++) {
        trace(args[i]);
    }
} 

traceArgArray.apply(this, [0].concat(myArray)); // [0] being the first argument (x)
Corey
  • 5,818
  • 2
  • 24
  • 37
1

See this

AS3 ... (rest) parameter

and this is referenced in the above:

AS3 arguments

and this could be useful as well

filling in (...rest) parameters with an array?

Community
  • 1
  • 1
Neoraptor
  • 951
  • 1
  • 7
  • 13
0

I think in this case it is more about handling the args then trying to call the method different...

    traceArgArray(1, 2, 3);

    var myArray:Array = new Array(1,2,3);   
    traceArgArray(1,myArray,"hello");


    myArray.push(["a",23,"her", 1.456, ["--","||",12,11] ]);
    traceArgArray(1,myArray,"hello");

    private var theArgsILike:Array= new Array();

    private function traceArgArray(x: int, ... args):void {

    //Creates the args you expect...
        theArgsILike.push(x);
        addArgs(args);

    // Trace both old and new style args
        trace("NEW Call: ------");
        trace("\n \targs: ------");
        for (var i:uint = 0; i < args.length; i++) {    
            trace(args[i]);
        }
        trace("\n \t The Args I Like: ------");
        for ( i = 0; i < theArgsILike.length; i++) {
            trace(theArgsILike[i]);
        }
        trace("\n");

        theArgsILike = new Array();
    }

    //splitting each array in its parts to add them on theArgsILike
    private function addArgs(inArgs:Array):void{
            //check if it is array 
        for each(var arg in inArgs){
                    if( Class(getDefinitionByName(getQualifiedClassName(arg))) == Class(getDefinitionByName(getQualifiedClassName(inArgs)))  ){
                    //spilt iteratively
                        addArgs(arg);
                    }
                    else{
                    // add arg 
                        theArgsILike.push(arg);
                    }
        }
    }
Sidrich2009
  • 570
  • 1
  • 4
  • 11