-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposed change to method group natural type #7364
Comments
Does this allows us to write : Console.WriteLine.M();
static class Ext
{
public static void M(this Action<string> action) { … }
} |
No. That scenario will not be affected (remains disallowed). |
Is this something that may be implemented in the future ? |
Yup. It's something we're actively looking into |
Nice. Thanks. |
If I understand this correctly, perhaps it would allow #129? |
The proposal in OP relates to which methods are considered to determine the natural type, but does not other affect how the methods are processed. Any scenario involving a single method (such as the |
The proposal was discussed and approved in LDM 2023-07-24 |
Closing as duplicate of #7429 which is already in the working set and has a speclet document draft. |
Determine natural type of method group by looking scope-by-scope
Speclet: #7399
For reference:
https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/lambda-improvements.md#natural-function-type
The current behavior leads to an arguably unnecessary error in the following scenario, where an irrelevant extension method (which we have no chance of binding to) causes an error on
z
.When the irrelevant extension method has the right signature, we can infer the type of the delegate, but we'll still bind to the instance method (ie. it wins over the extension method).
I would propose that we align the logic that infers the delegate type with the logic that ends up picking which method to bind to.
I would propose the following rule:
Note that we'll keep the following existing behavior, where we can infer the delegate type but cannot select which method to bind to:
One advantage of the current design is that it reduces surprises for users who change code from an explicit type to a
var
:If you start with
System.Action a = c.M;
, the method will have been picked by overload resolution. But when you switch tovar a = c.M;
the type is determined in a different way which can be jarring. So requiring all signatures to match reduces the surprise on the type. But it does not mitigate the surprise on which method we actually bind to...Relates to test plan dotnet/roslyn#52192 (lambda improvements) and dotnet/roslyn#66722 (extension types)
Design meetings
The text was updated successfully, but these errors were encountered: