[Proposal]: Improve method overload resolution for covariant value type arrays #3933
Replies: 3 comments 5 replies
-
i don't think we could change behavior here as there will be code that depends on existing semantics. You could certainly create your own analyzer that warns if you use this pattern if you think it's problematic for your domain. |
Beta Was this translation helpful? Give feedback.
-
Note that this is not really about "implicit" arrays, the code behaves the same if you explicitly specify the types: FindAsync(new int[] { 1, 2 }, CancellationToken.None); // still 1
FindAsync(new string[] { "1", "2" }, CancellationToken.None); // still 2 The difference is caused by array covariance: |
Beta Was this translation helpful? Give feedback.
-
Could probably work around this with a named argument, at least: FindAsync(new[] { 1, 2 }, token: CancellationToken.None); // 2 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Improve method overload resolution for arrays
Summary
The current implementation of method overload resolution seems to treat implicit arrays of value types differently that reference types. I propose that implicit arrays of value types should be treated the same as explicit arrays and implicit arrays of reference types.
Motivation
Why are we doing this?
To prevent the following runtime bug from happening:
What use case does it support?
Implicit value type arrays being passed to a method with multiple overloads. This is an issue that currently happens with
FindAsync
in EF Core:dotnet/efcore#22667
What is the expected outcome?
The method resolution logic should match that of explicit arrays and implicit reference type arrays.
Detailed design
If a method has multiple overloads, one with params of object[] and one with object[] and a second parameter, the second overload should be used when passing an implicit array and a second parameter that matches the type of the second parameter.
For example, this should be the outcome:
Drawbacks
It could break existing code with the very specific scenario of a method having two overloads, one with params of object[] and one with object[] and a second parameter.
Alternatives
This could possibly be an analyzer that warns users that they need to be more specific with their use of an API.
Unresolved questions
None
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions