www.digitalmars.com         C & C++   DMDScript  

D - Return type overloading

reply Marti <sip email.ee> writes:
Has function return type overloading been considered for D? It might be useful
for some scenarios.

For example, in the code::

int get2()
{
return 2;
}
float get2()
{
return 2.0;
}

the function body that gets called depends on the expected type of the value::

int i;
float f;

i = get2(); //the int version body of get2() is called
f = get2(); //the float version is called
f = cast(int)get2(); //the int version is called, and the value is implicitly
//casted to a float
f = cast(float)cast(int)get2(); //the int version is called, and the value is
//explicitly casted to a float

I'm not sure about what to do if the expected type can't be determined. Perhaps
we should require an explicit cast? Consider this::

f = 2 + get2();

I'm sure there are loads of other ambiguities I haven't thought about. I'm not
even sure if it's a good idea, but it's an idea worth considering nevertheless.

--
Marti
Jun 14 2004
parent Marti <sip email.ee> writes:
Oops, this message ended up in the old newsgroup. Please don't answer it here
and go to the post in digitalmars.D instead.

Sorry. :)

--
Marti
Jun 14 2004