Michael C said:
That's no big problem it should just give an error like it does with
current ambiguous overloads.
Currently, errors due to ambiguious overloads are quite rare, what you are
proposing would bring them into the forefront. You also have failed to
provide any workable syntax that differentiates between casting the return
value to a given type and selecting a given return type.
Now, I know it sounds easy, but its not. Overload selection is a complex
beast already, expanding it so that the method call is related to factors
outside of the method itself would compliate a compiler considerably.
Currently the language works from the inside out. To determine the
particular method the caller wants to call you only need the method call
itself:
Parse(something);
that tells the compiler everything it needs to know. The call expression is
simple, life is good.
Now, you add into that a requirement to know the type of the variable the
return type is going to be assigned to or the parameter type it will be
passed to. This forces the compiler to have to be able to see
int x = Parse(something);
The method call is not individually resolvable, it becomes an expression
that means nothing concrete without being contained within another
expression(I do not believe this issue exists *anywhere* else in the
language, but I'm not certain). The compiler is harder to write, the
language harder to learn. Bam, instant complexity at the base level of the
compiler.
Now, lets take this a step further and produce a few difficult or impossible
to resolve scenarios.
Given:
int Parse(string value);
double Parse(string value);
int Calc(int value);
double Calc(double value);
double d = Calc(Parse("15")):
When the compiler takes a look at the call to Parse it cannot resolve what
overload to call. The compile rmust then take a look at the parameter type
for Calc, which also cannot be resolved atomicall ywithout knowing the
resolved return type of the Parse call, so you have yet another unresolved
method. You step back a little farther and finally discover an assignment
meaning you can determine the desired return type of Calc, and in this
example, determine which overload of Parse must be called. However,
considering the compiler without return type overloads would have had no
trouble doing this, what is the cost in complexity?
Return type overloading where the return type is chosen based on assignment
is going to atleast double(and probably treble) the complexity of overload
resolution, which is already one of the trickest parts of the language.
Given:
int Parse(string value);
double Parse(string value);
string Parse(string value);
int Calc(int value);
double Calc(double value);
double Calc(string value);
double d = Calc(Parse("apple")):
In this circumstance the call to Parse cannot be resolved on its own, thus
the call to Calc cannot be resolved on its own, and since there are two calc
overloads that return double, resolution fails.
Is this really as simple as you think it is? I've not even started on the
possible issues with generics and anonymous methods and the language itself
is already considerably more complex. Realistically implementing return type
overloading, IMHO, requires a mechanism to manually select the override
instead of relying on automatic selection that ignores casts and
converstions.