function overload on full signature?

Rob T rob at ucora.com
Tue Nov 13 22:52:41 PST 2012


On Wednesday, 14 November 2012 at 02:01:56 UTC, Jonathan M Davis 
wrote:
> 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

I can envision simple ways for a programmer to supply hints to 
the compiler for easy resolution based on return type.

For example, when calling an overloaded function without 
assignment to anything:

int f() { ... }
void f() { ... }

f(); // should choose void return
(void)f(); // calls void return
(int)f(); // calls int return

In C++ there are conversion operators, which are not exactly the 
same as function overloading, but the correct function is 
selected based on the type on the left hand side.

Example

class A
{
    operator bool(){ return _b; }
    operator int(){ return _i; }
    int _i; bool _b;
}

A a;
bool b = a; // executes bool()
int i = a; // executes int()

Is there anything like C++ conversion operators in D? I have used 
conversion ops in C++ and may want to use a similar feature in D 
if available.


--rt


More information about the Digitalmars-d mailing list