Access keys

Overload

Kind of class: class
Inherits from: BasicClass
Author: Simon Wacker
Classpath: org.as2lib.env.overload.Overload
File last modified: Sunday, 21 August 2005, 15:00:54
Overload provides methods to overload a method.

With overloading you have typically two or more methods with the same name. Which
method gets actually invoked depends on its type signature, that means its return
and arguments' types. Here is an example of what overloading may look if it would be
supported by Flash (note that this code does actually not work).

Example:

// MyClass.as
class MyClass {
    public function myMethod(number:Number, string:String):Void {
        trace("myMethod(Number, String):Void");
    }
    public function myMethod(number:Number):Void {
        trace("myMethod(Number):Void");
    }
    public function myMethod(string:String):Number {
        trace("myMethod(String):Number");
        return 1;
    }
}

Usage:

// test.fla
var myInstance:MyClass = new MyClass();
myInstance.myMethod(1);
myInstance.myMethod(2, "myString");
var number:Number = myInstance.myMethod("myString");
trace(number);

Output:

myMethod(Number):Void
myMethod(Number, String):Void
myMethod(String):Number
1

As you can see, depending on what type the passed-in arguments have a different
method is invoked. This is sadly not possible with ActionScript, that is what this
class is for. Using the overload mechanism this class offers the overloading looks
as follows:

// MyClass.as
class MyClass {
    public function myMethod() {
        var o:Overload = new Overload(this);
        o.addHandler([Number, String], myMethodByNumberAndString);
        o.addHandler([Number], myMethodByNumber);
        o.addHandler([String], myMethodByString);
        return o.forward(arguments);
    }
    public function myMethodByNumberAndString(number:Number, string:String):Void {
        trace("myMethod(Number, String):Void");
    }
    public function myMethodByNumber(number:Number):Void {
        trace("myMethod(Number):Void");
    }
    public function myMethodByString(string:String):Number {
        trace("myMethod(String):Number");
        return 1;
    }
}

Using the above testing code the output looks the same.

While this is a good overloading mechanism / overloading alternative it still has
some disadvantages.

  • If not all methods the overloaded method forwards to returns a value of the
    same type, return type type-checking is lost.
  • The type checking of the arguments is also lost at compile time. At run-time the
    Overload class throws an UnknownOverloadHandlerException if the
    real arguments match no added overload handler.
  • The overloading slows the method execution a little bit down.

But if you declare the methods to overload to as public, as in the example, you
can still invoke them directly. Doing so, all the above problems do not hold true
anymore. The overloaded methods then acts more as a convenient method that is easy
to use if appropriate.

Constructor

Overload

function Overload (
target)
Constructs a new Overload instance.

The passed-in target is normally the object on which the overloading
takes place. This means it is the object that declares all methods that take
part at the overloading.

Parameters:
target:
the target to invoke the overloaded method on

Instance methods

addHandlerByHandler

function addHandlerByHandler (
handler:OverloadHandler) : Void
Adds the passed-in handler.

Overload handlers are used to determine the method to forward to. This is
done using the methods OverloadHandler.matches and
OverloadHandler.isMoreExplicit. If both conditions hold true the method
invocation is forwarded to the method of the handler, that gets returned by the
OverloadHandler.getMethod method.

If the passed-in handler is null or undefined no
actions will take place.

Parameters:
handler:
the new overload handler to add

addHandlerByValue

function addHandlerByValue (
argumentsTypes:Array, method:Function) : OverloadHandler
Adds a new SimpleOverloadHandler instance, that gets configured with the
passed-in argumentsTypes and method.

Overload handlers are used to determine the method to forward to. This is
done using the methods OverloadHandler.matches and
OverloadHandler.isMoreExplicit. If both conditions hold true the method
invocation is forwarded to the method of the handler, that gets returned by the
OverloadHandler.getMethod method.

The passed-in argumentsTypes are the types of arguments the method
expects from the real arguments to have. The SimpleOverloadHandler does
its matches and explicity checks upon these arguments' types.

The passed-in method is the method to invoke if the added handler
matches the real arguments and if it is the most explicit handler among all
matching ones.

Parameters:
argumentsTypes:
the arguments' types of the overload handler
method :
the method corresponding to the passed-in argumentsTypes
Returns:
the newly created overload handler

forward

function forward (
args:Array)
Forwards to the appropriate overload handler depending on the passed-in
args.

This is not done by using the OverloadHandler.execute method but
manually by using apply on the method returned by the
OverloadHandler.getMethod method. Invoking the method this way
increases the number of possible recurions with overlaoded methods.

If the args array is null or undefined an empty array
is used instead.

If no overload handler matches, the default overload handler will be used if
it has been set.

Overload handlers are supposed to have the same type signature if the
OverloadHandler.isMoreExplicit method returns null.

Returns:
the return value of the invoked method
Throws:
UnknownOverloadHandlerException if no adequate
overload handler could be found
SameTypeSignatureException if there exist at
least two overload handlers with the same type siganture, that means their
arguments' types are the same

getMatchingHandler

function getMatchingHandler (
args:Array) : OverloadHandler
Returns the most explicit overload handler from the array of matching handlers.

If the args array is null or undefined an empty array
is used instead.

If no handler matches the default handler gets returned if it has been set.

Overload handlers are supposed to have the same type signature if the
OverloadHandler.isMoreExplicit method returns null.

Parameters:
args:
the arguments that shall match to a specific overload handler
Returns:
the most explicit overload handler
Throws:
UnknownOverloadHandlerException if no adequate
overload handler could be found
SameTypeSignatureException if there exist at
least two overload handlers with the same type siganture, that means their
arguments' types are the same

removeDefaultHandler

function removeDefaultHandler (
Void) : Void
Removes the default handler.

This handler is used if no other handler matches to a list of arguments.

removeHandler

function removeHandler (
handler:OverloadHandler) : Void
Removes the passed-in handler.

All occurrences of the passed-in handler are removed.

Parameters:
handler:
the overload handler to remove

setDefaultHandler

function setDefaultHandler (
method:Function) : Void
Sets the default handler.

This handler will be used if no other handler matches to a list of arguments.
All real arguments used for the overloading are passed as parameters to the
method of this default handler.

The method is invoked on the same scope as the other handlers. That is the
target passed-in on construction.

var overload:Overload = new Overload(this);
overload.addHandler([String], methodWithStringArgument);
overload.addHandler([Number], methodWithNumberArgument);
overload.setDefaultHandler(function() {
    trace(arguments.length + " arguments were used.");
});
return overload.forward(arguments);

If the passed-in method is null, undefined or not of
type "function" the default handler gets removed.

Parameters:
method:
the method of the handler to invoke if no added handler matches
the overload arguments