views:

122

answers:

5

Let's Say I Have This Class:

package{
  import flash.display.Sprite;
  public class Main extends Sprite{
    public function Main(){
        trace(getAverage(1,2,3));
        trace(getAverage(1,2,3,4));
        trace(getAverage(1,2,3,4,5));
    }
    public function getAverage (...numbers) {
      var total = 0;
      for (var i = 0; i < numbers.length; i++) {
        total += numbers [i];
      }
      return total / numbers.length;
    }
  }
}

How do I accomplish the "opposite" of this? Namely, how could I now CALL 'getAverage' with a dynamic number of paraemters?

For instance, if I wanted to do something LIKE:

var r:int=Math.random()*6;
var a:Array=new Array();
for (i:int=0;i<r;i++) {
  a[i]=Math.random()*22;
}
// Now I have 'r' Number Of Parameters Stored In 'a'
//   How Do I Call getAverage, with all the values in 'a'??
//   getAverage(a) isn't right, is it?
//   I'm looking for something similar to getAverage(a[0],a[1],a[...]);

var av:Number=getAverage(???);

What I want to know, is if I have a function that takes a variable number of arguments, that's great, but how can I CALL IT with a variable number of arguments, when that number isn't known at runtime? Possibly it's impossible... I'm just not sure, since 'callLater' seems to be able to take an array and generate a dynamic number of parameters from it somehow...

NOTE: Answers consisting solely of "Why Do You Want To Do This?", will be downvoted.

P.S. This IS NOT about calculating Averages! I REALIZE There Are Way Simpler Ways Of Doing All Of This! (I could just write getAverage to accept a single array as its only parameter) The Above is just an EXAMPLE to Illustrate my Question. HOW TO PASS A DYNAMIC NUMBER OF PARAMETERS TO A FUNCTION?

+6  A: 

Is this what you're looking for?

var av:Number = getAverage.apply(null, a);
Dave
A: 

The problem with your question is that the arguments object is already an Array and using (...args) already provides you with a dynamic way to pass any number of arguments you require. Sorry about the previous answer, wasn't thinking straight...

PatrickS
A: 

You can create an array or an object with those parameters and pass that object to that function. That's just normal.

jase21
A: 

Flash has a rather strong introspection capabilities. So, instead of passing a number of objects, you just pass a single dynamic object with any number of attributes you need:

var ob:Object={arg1:"value1", arg2:8}; 
var arg:String="arg4";
ob["arg3"]=8;
ob[arg]=18;
trace (ob.hasOwnProperty("arg1"));
trace (ob.arg3);
trace (ob.arg4);

That should cover just about any use case you might need. The downside is that this allows for some rather clever and hard to trace bugs. :-)

jpop
+3  A: 

Dave is correct. You can use the apply method of a function to pass in an Array of arguments.

Here is a better explanation of how it works and what the arguments of apply are: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Function.html

Also note that you can use the call method to do the same thing but using ...args (comma-delimited list) instead, but apply would be more suitable to your situation.

Joony