June 14, 2004 Return type overloading | ||||
|---|---|---|---|---|
| ||||
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
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply