function overload on full signature?

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 13 18:01:43 PST 2012


On Tuesday, November 13, 2012 22:37:37 Peter Alexander wrote:
> On Tuesday, 13 November 2012 at 21:34:28 UTC, Rob T wrote:
> > I'm wondering why overloading has been implemented to only
> > match on the argument list rather than the full signature which
> > includes the return type? I know I would use it if it was
> > available.
> 
> If it worked on return type, how would it decide the overload?
> 
> int foo() { ... }
> string foo() { ... }
> 
> void bar(int x) { ... }
> void bar(string y) { ... }
> 
> auto z = foo(); // what foo?
> bar(foo()); // what foo?

It would also screw up covariant return types if overloading too the return 
type into account. I believe that the _big_ reason though is simply because 
the type of the expression is determined by the return type, not what it's 
assigned to. For instance, with how the language works, the type of the right-
hand side of an assignment must always be determined independently of the type 
on the left, so the type of the expression on the right-hand side cannot 
depend on the type of the left. I suspect that it would complicate things 
considerably to try and make the return type have anything to do with function 
overloading. I'm sure that you could find it being discussions on it somewhere 
online for other C-based laguages though.

Regardless, I've never even heard of a language which tried to include the 
return type in overload resolution. It sounds like a huge complication to me.

- Jonathan M Davis


More information about the Digitalmars-d mailing list