-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Avoid several breaking changes in overload resolution from inferred types of lambda expressions and method groups #56341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
eec2e0d
Add tests
cston a0909a1
Prefer overloads in 'better function member' where the conversion is …
cston 6afe3d8
Add tests
cston 1bc6a4e
Prefer overloads in 'better function member' where method type infere…
cston d7463d0
Misc.
cston f1c2eb1
Prefer overloads that did not require inferred type for type argument…
cston 6691280
Simplify MethodTypeInferrer
cston 98b8923
Misc.
cston 50608e2
Revert
cston 75ea2bd
PR feedback
cston 56bb01f
More tests
cston 49ac253
More tests
cston d237293
PR feedback
cston 269bb88
Add test
cston 78f781b
Document breaking changes
cston deed3d6
PR feedback
cston eea530e
Move several tests - no code change
cston cc7505a
Add test
cston 9f55dd5
Move test
cston 22e3187
Update breaking changes doc
cston b9bbbdb
Add indexer example
cston c4abad9
PR feedback
cston 1a38ee6
Update comment
cston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,104 @@ | |
{ | ||
if (o is null!) {} // error | ||
} | ||
``` | ||
``` | ||
|
||
2. In C# 10, lambda expressions and method groups with inferred type are implicitly convertible to `System.MulticastDelegate`, and bases classes and interfaces of `System.MulticastDelegate` including `object`, | ||
and lambda expressions and method groups are implicitly convertible to `System.Linq.Expressions.Expression` and `System.Linq.Expressions.LambdaExpression`. | ||
These are _function_type_conversions_. | ||
|
||
The new implicit conversions may change overload resolution in cases where the compiler searches iteratively for overloads and stops at the first type or namespace scope containing any applicable overloads. | ||
|
||
a. Instance and extension methods | ||
|
||
```csharp | ||
class C | ||
{ | ||
static void Main() | ||
{ | ||
var c = new C(); | ||
c.M(Main); // C#9: E.M(); C#10: C.M() | ||
c.M(() => { }); // C#9: E.M(); C#10: C.M() | ||
} | ||
|
||
void M(System.Delegate d) { } | ||
} | ||
|
||
static class E | ||
{ | ||
public static void M(this object x, System.Action y) { } | ||
} | ||
``` | ||
|
||
b. Base and derived methods | ||
|
||
```csharp | ||
using System; | ||
using System.Linq.Expressions; | ||
|
||
class A | ||
{ | ||
public void M(Func<int> f) { } | ||
public object this[Func<int> f] => null; | ||
public static A operator+(A a, Func<int> f) => a; | ||
} | ||
|
||
class B : A | ||
{ | ||
public void M(Expression e) { } | ||
public object this[Delegate d] => null; | ||
public static B operator+(B b, Delegate d) => b; | ||
} | ||
|
||
class Program | ||
{ | ||
static int F() => 1; | ||
|
||
static void Main() | ||
{ | ||
var b = new B(); | ||
b.M(() => 1); // C#9: A.M(); C#10: B.M() | ||
_ = b[() => 2]; // C#9: A.this[]; C#10: B.this[] | ||
_ = b + F; // C#9: A.operator+(); C#10: B.operator+() | ||
} | ||
} | ||
``` | ||
|
||
c. Method group conversion to `Expression` or `LambdaExpression` | ||
|
||
```csharp | ||
using System; | ||
using System.Linq.Expressions; | ||
|
||
var c = new C(); | ||
c.M(F); // error CS0428: Cannot convert method group 'F' to non-delegate type 'Expression' | ||
|
||
static int F() => 0; | ||
|
||
class C | ||
{ | ||
public void M(Expression e) { } | ||
} | ||
|
||
static class E | ||
{ | ||
public static void M(this object o, Func<int> a) { } | ||
} | ||
``` | ||
|
||
3. In C#10, a lambda expression with inferred type may contribute an argument type that affects overload resolution. | ||
|
||
```csharp | ||
using System; | ||
|
||
class Program | ||
{ | ||
static void F(Func<Func<object>> f, int i) { } | ||
static void F(Func<Func<int>> f, object o) { } | ||
|
||
static void Main() | ||
{ | ||
F(() => () => 1, 2); // C#9: F(Func<Func<object>>, int); C#10: ambiguous | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider breaking long lines #Closed