Overload by return type

Peter C. Chapin pcc482719 at gmail.com
Thu Jan 15 18:03:56 PST 2009


dsimcha wrote:

> Just curious, why doesn't D, and why don't more statically typed languages in
> general, support overload by return type?  I haven't exactly thought through
> all the pros and cons, but at first glance it seems like an incredibly useful
> thing.  What's the catch that I'm missing?

Ada allows overloading on return type. I think it can get away with it
because it does not allow any (hardly any) implicit type conversions.
Thus there is a great reduction in the complexity of the overload
resolution rules. That is, overload resolution in complicated
expressions is simplified because there are no implicit type conversions
 in the mix. As a small example:

procedure P(X : Integer);
function F(X : Integer) return Integer;
function F(X : Integer) return Float;

...

P(F(1));

The above can only mean a call of F returning Integer being passed to P
taking Integer since there is no way to use Float as an argument to P.
Of course ambiguities, when they arise, are flagged as errors as you
would expect. However, ambiguous situations don't arise as often as they
might because of the lack of implicit conversions.

Peter



More information about the Digitalmars-d mailing list