Method overloading using namespaces

June 15, 2007 on 5:21 pm | In Actionscript | 3 Comments

It’s too bad that AS3 can’t handle method overloading natively, but there is a way to add at least similar functionality using namespaces. True overloading capability means that the compiler figures out which function to use based on the given arguments. Here you have to help the compiler by giving the appropriate namespace tag:

package
{
    import flash.display.Sprite;

    public class MethodOverloadingExample extends Sprite
    {
        //three methods: three namespaces
        public namespace m1;
        public namespace m2;
        public namespace m3;

        public function MethodOverloadingExample():void
        {
            m1::overloadedMethod(1);
        }

     m1 function overloadedMethod(arg0:int):void
        {
            trace("method 1, args=" + arguments.join("|"));

            var out:Boolean = m2::overloadedMethod(1, 2);
        }

     m2 function overloadedMethod(arg0:int, arg1:int):Boolean
        {
            trace("method 2, args=" + arguments.join("|"));

            var out:Number = m3::overloadedMethod(1, 2, 3);
            return true;
        }

     m3 function overloadedMethod(arg0:int, arg1:int, arg2:int):Number
        {
            trace("method 3, args=" + arguments.join("|"));
            return 3;
        }
    }
}

After instantiating the class the various trace calls print out this:

overloaded method 1, args=1
overloaded method 2, args=1|2
overloaded method 3, args=1|2|3

All methods can have different signatures (number of arguments and return type). Nested ‘overloaded’ method calls are allowed, too. I ran a lot of benchmarks and it turned out that namespaces don’t affect performance at all, so you can use this technique for time-critical situations as well.

If you want to access the methods from outside the class you first have to retrieve the namespace and make the call through it. Somehow you can’t access a namespace directly without getting complains from the compiler, so create a get function first.

//inside the class
public function getNameSpace1():Namespace
{
    return m1;
}
...

//outside the class
var m1:Namespace = obj.getNameSpace1();
obj.m1::overloadedMethod(0);

3 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Yeh it has always been a bummer not to be able to overload in Actionscript. Many times we have .NET/JAVA libs we expose to remoting and have to either change the calls a bit or it influences our design at ther service level.

    Sometimes just planning on dealing without them is best but this is worth a shot with namespaces.

    Comment by draw.logic — June, 18 2007 #

  2. Nice one :-)
    I second draw.logic comment, it’s a pity they didn’t include overloading.
    I will give it a try. Thanks for the useful tips and blog.

    Comment by Lapo — July, 9 2007 #

  3. Hey super cool..

    Think i can live without overloading, but still a nice workaround.

    Off course you can also always just make the …params as paramaters og send an object with the args or an XML, wich you then can tjeck for what params you get…

    Comment by fedlarm — October, 31 2008 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Proudly powered by WordPress Theme based upon Pool theme by Borja Fernandez.
Entries and comments feeds.