views:

166

answers:

1

I have a vararg method that I'd like to act as a proxy for another vararg method, but I'm not sure how to do it. Here's the basic code:

class MyClass {

   public function a(...args:*):* {
      // other code
      b(args);
      // other code
   }

   public function b(...args:*):* {
      // do stuff with args
   }

}

I'm porting the code from Java, where the type system knows that the arguments are actually supposed to be Strings, not arrays, so it can figure out to invoke b() by directly passing the arguments, and everything works how you'd expect.

But in ActionScript, the argument array gets wrapped into another array when invoked through the proxy method.

So, when b() is called directly, the array is only one level deep. But when b() is invoked through a(), the array is two levels deep.

Does anyone know of a trick for getting around this?

(NOTE: in my real code, a() and b() are actually in separate classes, and I really don't want to touch the implementation of b() at all. I can rewrite a() to my heart's content, but b() shouldn't change.)

+1  A: 

Well I can't claim to be any good at ActionScript (haven't used it in years..)

But if nothing else you can do something like..

class Test {
    function a(...args:*):* { b.apply(this, args); }
    function b(...args:*):* { trace(args[1]); }
}

In previous versions you could use 'arguments' to pass all passed arguments through the apply method, but that seems to have been removed in AS3.

Sciolist
Wow. That's wild. I had no idea that was possible. Thanks a million!!!
benjismith
`arguments` is still there, it's just missing the `caller` property: http://livedocs.adobe.com/flex/3/langref/arguments.html
hasseg
Oh? I tried it out and kept getting that it was undefined.
Sciolist