Array of references rather than values

Krinlon, you answered my previous question about arrays by showing how to get a reference to a variable out of an array, rather than its value. I did not understand your answer so I’m going to ask a more specific question and see if you can fill in the blanks for me.

Consider the following:

var incr:int = 0;
var arr:Array = [myFunction, getArgument];
function myFunction(arg):int {
     return arg;
}
function getArgument():int {
     return incr++;
}
trace(arr[0](arr[1]));    // 0
trace(arr[0](arr[1]));    // 0
trace(arr[0](arr[1]));    // 0

I understand this happens because the second element of arr is simply a reference to the value returned by getArgument at the time the array is created. (Is that the best way to say that?)

But I want getArgument to be executed every time, so that::

trace(arr[0](arr[1]));    // 0
trace(arr[0](arr[1]));    // 1
trace(arr[0](arr[1]));    // 2

How do I do that?

Nope. Its happening because you’re passing in arr[1] to the function call of myFunction (as arr[0]) which resolves to the getArgument function itself, not what you get when you call getArgument. And that is getting coerced into an int because that’s what you specified as the return value. What you want to be doing is

trace(arr[0]( arr[1]() ));

which will get you what you want (note the added parens given to arr[1] so its being called).

senocular’s pretty much got this covered, I think.

Sorry. Sometimes it’s really hard to map other people’s use of terminology with my own understanding of it.