Overload Resolution via Return Types #8086
Unanswered
TonyValenti
asked this question in
Language Ideas
Replies: 1 comment 2 replies
-
It's interesting, and technically possible, but given that overloading is already super convoluted, influenced by all kinds of type coercion rules, I can't imagine that it's worth adding the complexity of trying to also disambiguate candidates through different return types. What are the use cases for supporting something like this? I don't think you'd design a public API that would use a feature like this. IMO I think the only use cases where it might be useful is where you want to deprecate an API and replace it with another one, but don't want to break existing clients. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When #7706 gets implemented, I will be able to control which overloads the compiler picks. However, this does not solve one problem: allowing me to pick a method that varies only by return type. ie:
In conjunction with #7706, the language should allow methods to vary by only return type.
Selecting the Correct Method via Type Inferencing
The correct method can be selected via Type Inferencing where the context permits. For example:
and:
will automatically select
public static string? Test()
Selecting the Correct Method via Hints
In the event that an ambiguity still exists, a 'hint' can be provided that allows selecting the preferred overload.
Hints look just like a cast:
If you want to cast the value of the abiguous method, you'll 'cast' it twice
Discussion
The double cast syntax might seem odd at first, but the language already requires this is some situations. Specifically when working with generics:
There is also zero chance that the double-cast syntax will conflict with cause issues with existing code because methods that only vary by return type currently do not exist.
Beta Was this translation helpful? Give feedback.
All reactions