On 2009-11-19, at 16:11, Rami Ojares / AMG Oy wrote:

> One more thing that has frequently crossed my mind.
> According to my understanding methods are identified solely by their name (in 
> edition 3).
> 
> So it is illegal to have two methods like
> 
> <method name="foo" args="a1">
> ...
> </method>
> 
> <method name="foo" args="a1, a2">
> ...
> </method>
> 
> Now that edition 4 requires always using the right amount of arguments (and 
> even adds types to arguments) are the methods identified there by their name 
> plus their argument list (like in java for example) or are they still 
> identified solely by their name?

as3 and edition 4 both require overriding methods to be congruent, meaning they 
must have the same type signature.  To put it in the terms you have used, 
methods are defined solely by their name, still.  There are type-theoretic 
arguments as to why this is considered the "right thing" and why C++ and its 
descendants, despite possibly having the widest deployment of O-O languages are 
considered wrong.  I can give you references if you are interested, but are 
three points that may be sufficient:

1) In C++ descendants, the dispatch on argument signature is implemented at 
compile time (effectively, the compiler renames every method to a name that is 
a reversible encryption of its name and signature, so the methods at runtime 
are unique).  The opposing camp is that the language has a uniform model of 
dispatch that can be described (and implemented) by simple runtime semantics, 
which the compiler may optimize (if it can prove that the optimization obeys 
the defined semantics).

2) If you consider the signature of a method as defining the contract of the 
method, then having another method with a different signature implies a broken 
contract.  These are really two different methods and should have different 
names (as C++ descendents admit under the covers).

3) In my experience, 99% of the uses of "method overloading" (as it is called 
in C++), is simply to implement default arguments.  Typically you might have a 
method that takes two arguments, the second of which is usually always the same 
value.  In C++ you would write:

  RetType name (arg1:A1Type, arg2:A2Type) { ... }
  RetType name (arg1:A1Type) { return name(arg1, arg2Default); }

In OpenLaszlo, you write:

  <method name="name" args="arg1:A1Type, arg2:A2Type = arg2Default" 
returns="RetType"> ... </method>

or in script:

  function name (arg1:A1Type, arg2:A2Type = arg2Default):RetType { ... }

Which to me seems much more straightforward.

If you really need methods with different signatures, other than for creating 
optional arguments, it most likely means that you have separate APIs and you 
really just need to think up better (more descriptive) names for your methods.  
[Worst case you can name them the same way the C++ compiler would name them, by 
adding a representation of their arguments signature to the name.  But you can 
probably come up with a more meaningful and descriptive name than the compiler 
can.]

Reply via email to