Add proposal for type inference from method group - #10239
Conversation
|
|
||
| The current [type inference rules](https://github.com/dotnet/csharpstandard/blob/draft-v7/standard/expressions.md#1263-type-inference) allow | ||
| - method groups to contribute to output type inference | ||
| - infering bounds for the `Ui` type parameters in `C<U1 ... Uk>` when given a `C<V1...Vk>` and `C` is a class, struct, interface or delegate type. |
There was a problem hiding this comment.
| - infering bounds for the `Ui` type parameters in `C<U1 ... Uk>` when given a `C<V1...Vk>` and `C` is a class, struct, interface or delegate type. | |
| - inferring bounds for the `Ui` type parameters in `C<U1 ... Uk>` when given a `C<V1...Vk>` and `C` is a class, struct, interface or delegate type. |
| ## Summary | ||
| [summary]: #summary | ||
|
|
||
| It allows the natural type of a method group to contribute to method type inference |
There was a problem hiding this comment.
| It allows the natural type of a method group to contribute to method type inference | |
| It allows a method group with a unique signature to contribute to method type inference. |
Because as written I think the spec would still work even if method groups had no natural type.
|
|
||
| ## Design | ||
|
|
||
| We modify the [explicit parameter type inference rules](https://github.com/dotnet/csharpstandard/blob/draft-v7/standard/expressions.md#12638-explicit-parameter-type-inferences) to not just apply to explicitly-typed lambdas, but also to method groups: |
There was a problem hiding this comment.
Perhaps expand on potential breaking changes that will come from this? IIRC that was a major reason we didn't do this in the first place.
There was a problem hiding this comment.
E.g. the exact inference causes:
static bool P(object o) => true;
static void M<T>(Func<T, bool> f, T value) => Console.WriteLine(typeof(T));
M(P, "hello"); // Today, prints System.String; after proposal, prints System.Object
M(bool (object o) => true, "hello"); // Prints System.ObjectThere was a problem hiding this comment.
Similarly, this would stop compiling:
static bool P(object o) => true;
static void M<T>(Func<T, bool> f, Func<T, bool> g) { }
M((string s) => true, P); // Compiles now, would behave like the next line after the proposal
M((string s) => true, bool (object o) => true); // Fails to compileThere was a problem hiding this comment.
Those are good to include, but neither would have worked before the original lambda changes. The breaks I'm referring to are when an inner scope method suddenly becomes applicable where it wasn't before, such as an instance generic now working instead of going to an extension method, or a derived overload now working where previously it went to a base type.
Relates to championed issue: #9007