ActionScript 3: Overloading constructors with …(rest)

ActionScript 3 allows a function to have a variable number of arguments by using so-called … (rest) parameter. Ellipses followed by the name represent an array parameter that can contain any number of comma-delimited arguments:

public static function calcTax(… taxParams):Number{
for (uint i=0; i< taxParams.length; i++){ trace(taxParams[i]); } }

Java programmers may find the … (rest) similar to the varargs notation. You can mix the … (rest) with other function parameters as long as it’s the last parameter listed.

Unless you use the rest parameter, AS3 creates in memory a special object called arguments, which is an array that include references to each of the arguments and a reference to function itself (arguments.callee). AS3 allows you to call a function with more parameters than were included in the function declaration, and each of the parameters can be access by using the arguments[i] notation.

Here’s an idea of using … (rest) parameter to overcome the absence of overloaded constructors in AS3:

public function MyCLass(…args) {
switch (args.length) {
case 0: constructor1(); return;
case 1: constructor2(args[0]); return;
case 2: constructor3(args[0], args[1]); return;

} }

This sample covers the case of constructors having different number of parameters. But if you want this solution to work with functions having the same number of parameters but different types, you’d need to add the type check to each of these cases above, i.e.

if(args[0] is String) {
//do one thing
}else if (args[0] is Number){
// do another thing

6 thoughts on “ActionScript 3: Overloading constructors with …(rest)

  1. I would prefer a native overload support, or at least a keyword arguments support like in python, which let you to pass arguments not as an ordered array, but using name/value pairs…

  2. I think that ordered array can be useful too. name/value can bring a ordered way to work with arguments. Great stuff from both of you guys.

  3. It’s worth noting that the simulated over-clocking method outlined above via the arguments object works just as well in ActionScript 2.0, where it originated. If used sparingly in a well-architected Flash app where contracts between objects are clearly outlined, it works quite well. I’m curious, though, not having jumped the fence to AS 3.0 yet, how is … (rest) any different or better than me defining a parameter of type Array myself in a method and passing it an associative array when I call it?

    Also, you said that … (rest) can be used with normal parameters; doesn’t that mean that an arguments object is created anyway? I’m confused on that point. If not, I think I can vaguely see the point of … (rest) in that you could pass any number of parameters without having to explicitly reference the arguments object. You would still have to reference the parameter defined by … (rest), so it sort of seems like a pointless substitution in logical terms, but I suppose “taxParams[0]” would tell other developers more than “arguments[0]”. However, you can still define an Array parameter in your declaration that would serve the same purpose as long as you passed it surrounded by array operators and placed it last in your declaration. Personally, I’d rather just use the arguments object.

  4. But is this really worth doing at all? The whole point of an overloaded function is that it makes it easier and more intuitive to use, but this just jumbles up the code and makes things too complex. I think it’s better to just use different function names as it doesn’t obfuscate the code and make the code jump all over the place. I still really wish there was native overloading support in actionscript though…

  5. It’s better to have native support of overloaded functions if you are planning to use polymorphism techniques in your program. Having functions with different names won’t let you do this.

Comments are closed.