Dan Sugalski wrote:
> There's also the fun of:
> 
>     Dog bar(int);
>     Cat bar(int);
> 
> and
> 
>     xyzzy(Dog);
>     xyzzy(Cat);
> 
> with the call of:
> 
>     xyzzy(bar(1));
> 
> Just one of the many brain-benders that I'm glad Larry has to deal
> with, not me. (Though this may be one of the reasons A6 is taking so
> long...)
> --

Ada handles this kind of problem. This is what used to be called operator overloading, 
before the phrase was coopted by C++ (before C++ came along, the limited version of 
operator overloading used in C++ was called *operand* overloading, since you're only 
allowed to overload based on operand types).

To disambiguate a call in the presence of full operator overloading, two full complete 
passes over the expression tree are required. The first pass is a bottom-up pass to 
collect all the potential return types; the second is a top-down pass that uses the 
context to eliminate some (hopefully all but one) of the available return types 
computed in the first pass. In Ada, if the second pass doesn't completely disambiguate 
the expression, it's considered an error.

I studied this stuff for my masters thesis, for which I added (Ada style) operator 
overloading to a Modula-2 compiler. When it was all over, I decided that I much prefer 
(C++ style) operand overloading. The computation to decide what the programmer meant 
is too complicated. For the user, not the compiler. Although the compiler can do it 
[the code is bulky, but not difficult to understand], I think that the programmer will 
be left in the dust. And I think that's a bad thing. In all but the simplest cases, 
the coder will have a very difficult time figuring out what the compiler's actually 
going to do.

As a matter of fact, I always thought that C<wantarray> was a nice compromise: let the 
programmer decide exactly what to do in the small number of cases where it's really 
useful.

So here's *my* vote against return-type multi-method disambiguation.

=thom
"Don't use that word [fantastic] to a lawyer; straining at gnats and swallowing camels 
is a required course in law school...." -- _Stranger_in_a_Strange_Land_

Reply via email to