csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

[Proposal]: Type Parameter Inference from Constraints

Open 333fred opened this issue 6 months ago • 0 comments

Type Parameter Inference from Constraints

  • Specification: https://github.com/dotnet/csharplang/pull/9455
  • Discussion: https://github.com/dotnet/csharplang/discussions/9454

Summary

Allow type inference to succeed in overload resolution scenarios by promoting generic constraints of inferred type parameters to "fake arguments" during type inference, enabling the bounds of type variables to participate in the inference process. An example is:

List<int> l = [1, 2, 3];
M(l); // Today: TElement cannot be inferred. With this proposal, successful call.

void M<TEnumerable, TElement>(TEnumerable t) where TEnumerable : IEnumerable<TElement>
{
    Console.WriteLine(string.Join(",", t));
}

Design meetings

  • https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-06-30.md#type-parameter-inference-from-constraints
  • https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-08-18.md#type-inference

333fred avatar Jun 17 '25 00:06 333fred