Why don't more mainstream statically typed languages support function/method overloading by return type? I can't think of any that do. It seems no less useful or reasonable than supporting overload by parameter type. How come it's so much less popular?
相关问题
- Dynamic Casts or Function Overloads?
- C++ operator lookup misunderstanding
- How to parse nested function calls using Pyparsing
- overloading a float to a numpy array
- Is divide by zero an error or an exception?
相关文章
- Generics and calling overloaded method from differ
- Overloading a super class's function
- Are there any reasons not to use “this” (“Self”, “
- Call/Return feature of classic C++(C with Classes)
- What are the major differences between C and C++ a
- Scala: arrays and type erasure
- Overload resolution with extern “C” linkage
- What set of functions is considered when resolving
In haskell it's possible even though it doesn't have function overloading. Haskell uses type classes. In a program you could see:
Function overloading itself is not so popular. Mostly languages I've seen with it are C++, perhaps java and/or C#. In all dynamic languages it's a shorthand for:
Therefore there's no much point in it. Most people aren't interested whether language can help you drop a single line per where ever you use it.
Pattern matching is somewhat similar to function overloading, and I guess sometimes work similarly. It's not common though because it is useful only for few programs and is tricky to implement on most of languages.
You see there's infinitely many other better easier-to-implement features to implement into the language, including:
In .NET, sometimes we use one parameter to indicate the desired output from a generic result, and then made a conversion to get what we expect.
C#
Maybe this example could help too:
C++
To steal a C++ specific answer from another very similar question (dupe?):
Function return types don't come into play in overload resolution simply because Stroustrup (I assume with input from other C++ architects) wanted overload resolution to be 'context independent'. See 7.4.1 - "Overloading and Return Type" from the "C++ Programming Language, Third Edition".
They wanted it to be based only on how the overload was called - not how the result was used (if it was used at all). Indeed, many functions are called without using the result or the result would be used as part of a larger expression. One factor that I'm sure came into play when they decided this was that if the return type was part of the resolution there would be many calls to overloaded functions that would need to be resolved with complex rules or would have to have the compiler throw an error that the call was ambiguous.
And, Lord knows, C++ overload resolution is complex enough as it stands...
if you want to overload methods with different return types, just add a dummy parameter with default value to allow the overload execution, but don't forget the parameter type should be different so the overload logic works next is an e.g on delphi:
use it like this
Good answers! A.Rex's answer in particular is very detailed and instructive. As he points out, C++ does consider user-supplied type-conversion operators when compiling
lhs = func();
(where func is really the name of a struct). My workaround is a bit different - not better, just different (although it's based on the same basic idea).Whereas I had wanted to write...
I ended up with a solution that uses a parameterized struct (with T = the return type):
A benefit of this solution is that any code which includes these template definitions can add more specializations for more types. Also you can do partial specializations of the struct as needed. For example, if you wanted special handling for pointer types:
As a negative, you can't write
int x = func();
with my solution. You have to writeint x = func<int>();
. You have to explicitly say what the return type is, rather than asking the compiler to suss it out by looking at type conversion operators. I would say that "my" solution and A.Rex's both belong in a pareto-optimal front of ways to tackle this C++ dilemma :)I think this is a GAP in modern C++ definition… why ?
Why can a C++ compiler can not throw an error in example "3" and accept the code in example "1+2" ??