-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Proposal]: Type inference using method group natural type #7687
Copy link
Copy link
Closed
Description
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
Uitype parameters inC<U1 ... Uk>when given aC<V1...Vk>andCis 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
Eis an explicitly typed anonymous function *or method group with a unique signature with parameter typesU₁...UᵥandTis a delegate type or expression tree type with parameter typesV₁...Vᵥthen for eachUᵢan exact inference is made fromUᵢto the correspondingVᵢ.
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Active/Investigating