Skip to content
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

[Proposal]: Type inference using method group natural type #7687

Open
1 of 4 tasks
jcouv opened this issue Nov 14, 2023 · 0 comments
Open
1 of 4 tasks

[Proposal]: Type inference using method group natural type #7687

jcouv opened this issue Nov 14, 2023 · 0 comments
Assignees

Comments

@jcouv
Copy link
Member

jcouv commented Nov 14, 2023

Type inference using method group natural type

  • Proposed
  • Prototype: Started
  • Implementation: Started
  • Specification: Not Started

Summary

It allows the natural type of a method group to contribute to method type inference

Context on method type inference

The current type inference rules 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.
    But they don't allow method groups to contribute to bounds.
Test(IsEven); // Error CS0411	The type arguments for method 'Program.Test<T>(Func<T, bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

partial class Program
{
    public static bool IsEven(int x) => x % 2 == 0;
    public static void Test<T>(Func<T, bool> predicate) { }
}

Type inference

We modify the explicit parameter type inference rules to not just apply to explicitly-typed lambdas, but also to method groups:

An explicit parameter type inference is made from an expression E to a type T in the following way:

  • If E is an explicitly typed anonymous function *or method group with a unique signature with parameter types U₁...Uᵥ and T is a delegate type or expression tree type with parameter types V₁...Vᵥ then for each Uᵢ an exact inference is made from Uᵢ to the corresponding Vᵢ.

Follow-ups:

  • we need to confirm this was not left out intentionally from the "lambda improvements" work
  • outline and review any potential breaking changes

Relates to issue for method groups contributing to method type inference: #129

LDM Discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Active/Investigating
Development

No branches or pull requests

2 participants