Open 333fred opened 4 years ago
Speaking of overload resolution, any chance https://github.com/dotnet/csharplang/issues/129 could be considered too? Though it's more about type inference, I'd say it is aligned with the motivation stated here.
Maybe related: https://github.com/dotnet/csharplang/discussions/4303
No, I don't believe that's related.
Improve overload resolution for delegate compatibility
Summary
Improve overload resolution when looking for applicable methods by removing methods that cannot be compatible. This will allow the following code to compile (it does not today):
Motivation
In the spirit of the other overload resolution changes we've made recently, removing things that already don't work, this would allow currently ambiguous code to compile and improve user experience.
Detailed design
Given the example in the Summary section, the reason why it does not compile today is straightforward, if requiring an intimate knowledge of the spec to figure out:
D
. To do that, we start by computing the applicable function members in the method groupD
.D
if the method inD
is applicable.D(MyAction<int>)
, we need to determine if the method groupY
is applicable toMyAction<T>
whereT
isint
.Y
.Y
, which is of typelong
. There is an implicit numeric conversion fromint
tolong
. There are no more parameters, soY
is applicable toMyAction<int>
and a method group conversion exists.Y
is not compatible withMyAction<int>
, it is applicable.D(MyAction<int>)
is an applicable member.D(MyAction<long>)
. The same sub-steps run again, andD(MyAction<long>)
is determined to be applicable.All of this occurs despite the fact that we can unambiguously determine that
D(MyAction<int>)
can never be a valid target forD(Y)
:Y
isn't compatible withMyAction<int>
. This is because the applicable function member algorithm looks for any implicit conversion for parameter types, and delegate compatibility looks only for implicit reference conversions. The proposed change, then, is to the first step of the method group conversions section of the spec (addition is italicized):Drawbacks
Any change is work.
Alternatives
Doing nothing.
Unresolved questions
N/A
Design meetings