Skip to content

Add proposal for type inference from method group - #10239

Open
jcouv wants to merge 1 commit into
dotnet:mainfrom
jcouv:method-group
Open

Add proposal for type inference from method group#10239
jcouv wants to merge 1 commit into
dotnet:mainfrom
jcouv:method-group

Conversation

@jcouv

@jcouv jcouv commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Relates to championed issue: #9007

@jcouv jcouv self-assigned this Jun 19, 2026
@jcouv
jcouv marked this pull request as ready for review June 19, 2026 16:16
@jcouv
jcouv requested a review from a team as a code owner June 19, 2026 16:16
@jcouv
jcouv requested a review from jnm2 June 19, 2026 16:16

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jnm2 jnm2 Jun 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 compile

@333fred 333fred Jun 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants