Proposal: Implicit overloads for return value overloading #8003
Replies: 4 comments 2 replies
-
Feels related to (but in no way duplicates) #7706 |
Beta Was this translation helpful? Give feedback.
-
It might be possible to use the keyword |
Beta Was this translation helpful? Give feedback.
-
I think that if we allowed overloading on return type with the restriction that the new proposed |
Beta Was this translation helpful? Give feedback.
-
@hez2010 can you please provide another example where this is useful? I'm not sure the regex match groups is a good candidate - unless I'm missing something. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
Currently, we have a lot of APIs that are not modernized, for example,
GetEnumerator
which returns a non-generic collection, the non-genericGroups
property in regex matching result, and thestring.Split()
which returns astring[]
instead ofReadOnlySpan<ReadOnlySpan<char>>
(should be possible after we support ref structs in generic arguments).We cannot just create a new overload with different return value types in C# today, but fortunately, CIL supports overloading by return values, and we are including return values in the signature and taking it into account while doing method lookup.
Overloading by return values
We can enable overloading by return values to allow a method to be overloaded by the type of return value.
But this soon makes adding an overload a source breaking change as the compiler can no longer figure out which method to use if the receiver is implicitly typed (such as
var
).Implicit overloads
We can resolve the above issue by introducing implicit overloads:
If the receiver is implicitly typed (like
var
), the compiler then picks the implicit one to ensure the existing usage won't break. If the receiver is explicitly typed, it follows the existing overload resolution rules to pick the best one.Beta Was this translation helpful? Give feedback.
All reactions