org.as2lib.core.BasicClass +--org.as2lib.env.overload.SimpleOverloadHandler
OverloadHandler
SimpleOverloadHandler
offers basic overloading functionalities.
Overload handlers are used by the Overload
class to identify the
corresponding method for a specific list of arguments. Whereby the overload handler
holds the method and the expected arguments' types of this method.
It also offers functionalities to match real arguments against the expected arguments' types, matches, and to determine which overload handler or rather which arguments' types of two handlers are more explicit, isMoreExplicit.
It also offers the ability to invoke/execute the target method on a target scope passing-in a list of real arguments.
This class is normally not used directly but indirectly via the Overload#addHandler method.
If you nevertheless want to instantiate it by hand and then use it with the
Overload
class you can do this as follows:
this.myMethod = function(number:Number, string:String):String {
return (number + ", " + string);
}
var overload:Overload = new Overload(this);
var handler:OverloadHandler = new SimpleOverloadHandler([Number, String], myMethod);
overload.addHandler(handler);
trace(overload.forward([2, "myString"]));
Note that the handlers arguments signature (the arguments' types) match exactly
the ones of the method myMethod
.
public function SimpleOverloadHandler(argumentsTypes:Array, method:Function)
Constructs a new SimpleOverloadHandler
instance.
If the passed-in argumentsTypes
array is null
or
undefined
an empty array is used instead.
The passed-in argumentsTypes
are the types of arguments this handler
expects the real arguments to have. The arguments' types thus are also the types
of arguments the method, this handler forwards to, expects. The matches
and isMoreExplicit methods do their job based on the arguments' types.
An argument-type is represented by a class or interface, that is a
Function
in ActionScript. An argument type can for example be
Number
, String
, org.as2lib.core.BasicClass
,
org.as2lib.core.BasicInterface
or any other class or interface.
An argument-type of value null
or undefined
is interpreted
as any type allowed and is less explicit then any other type.
The arguments' types determine what method call is forwarded to this handler
which then invokes the passed-in method
. The forwarding to this handler
normally takes place if it's matching the passed-in real arguments,
matches, and if it is the most explicit overload handler,
isMoreExplicit.
argumentsTypes | the arguments' types of the method |
method | the actual method to execute on the target if the argumetns' types match |
IllegalArgumentException | if the passed-in method is null
or undefined
|
public function matches(realArguments:Array):Boolean
Checks whether the passed-in realArguments
match the arguments' types
of this overload handler.
If the passed-in realArguments
array is null
or
undefined
, an empty array is used instead.
If a real argument has the value null
or undefined
it matches
every type.
If the expected argument-type is null
or undefined
it matches
every real argument. That means null
and undefined
are
interpreted as Object
, which also matches every real argument.
realArguments | the real arguments to match against the arguments' types |
true
if the real arguments match the arguments' types else
false
public function execute(target, args:Array)
Executes the method of this handler on the given target
passing-in the
given args
.
The this
scope of the method refers to the passed-in target
on execution.
target | the target object to invoke the method on |
args | the arguments to pass-in on method invocation |
the result of the method invocation
public function isMoreExplicit(handler:OverloadHandler):Boolean
Checks if this overload handler is more explicit than the passed-in
handler
.
The check is based on the arguments' types of both handlers. They are compared one by one.
What means more explicit? The type String
is for example more
explicit than Object
. The type org.as2lib.core.BasicClass
is
also more explicit than Object
. And the type
org.as2lib.env.overload.SimpleOverloadHandler
is more explicit than
org.as2lib.core.BasicClass
. I hope you get the image. As you can see,
the explicitness depends on the inheritance hierarchy.
Note that classes are supposed to be more explicit than interfaces.
handler
is null
true
will be
returned. handler
's getArguments
method returns
null
an empty array will be used instead. true
will be returned. null
it is less explicit than no matter
what type it is compared with.handler | the handler to compare this handler with regarding its explicitness |
true
if this handler is more explicit else false
or
null
if the two handlers have the same explicitness
public function getArgumentsTypes(Void):Array
Returns the arguments' types used to match against the real arguments.
The arguments' types determine for which types of arguments the method was declared for. That means which arguments' types the method expects.
the arguments' types the method expects
getArgumentsTypes() in org.as2lib.env.overload.OverloadHandler
public function getMethod(Void):Function
Returns the method this overload handler was assigned to.
This is the method to invoke passing the appropriate arguments when this handler matches the arguments and is the most explicit one.
the method to invoke when the real arguments match the ones of this handler and this handler is the most explicit one
public function toString():String
Returns a detailed string representation of this overload handler.
The string representation is composed as follows:
[object SimpleOverloadHandler(firstArgumentType, ..)]
toString() in org.as2lib.core.BasicInterface