Roslyn: Type argument inference with unambiguous method group

Created on 30 Jan 2017  路  5Comments  路  Source: dotnet/roslyn

Apologies if this is a dupe; I don't see another issue like this.

Please allow the following. I see no possible ambiguity in this scenario. TheIsEven and Test method groups have only one overload which fits the delegate signature.

```c#
class Program
{
public static void Main()
{
Test(IsEven);
}

public static bool IsEven(int x) => x % 2 == 0;

public static void Test<T>(Func<T, bool> predicate)
{
}

}
```

Expected Behavior:
Infers Test<int>(IsEven) and compiles

Actual Behavior:
CS0411 The type arguments for method 'Program.Test(Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Area-Language Design

Most helpful comment

Type inference doesn't infer based on an argument that is a method group, no matter how many or few members the method group has. The conversion from a method group to a delegate (of some particular type) occurs after type inference has completed to determine what that delegate type is.

Having said that, I imagine it might be possible to modify (the specification for) type inference to get the behavior @jnm2 would like. Who would like to take a crack at it?

All 5 comments

Believe this is by design but will let @gafter comment on exactly how.

Type inference doesn't infer based on an argument that is a method group, no matter how many or few members the method group has. The conversion from a method group to a delegate (of some particular type) occurs after type inference has completed to determine what that delegate type is.

Having said that, I imagine it might be possible to modify (the specification for) type inference to get the behavior @jnm2 would like. Who would like to take a crack at it?

@gafter Should I move this to csharplang?

@jnm2 Sure, if you'd like to discuss it further. But without a suggestion as to how it would work, I don't see how it can move past the discussion phase. Perhaps that discussion will trigger an idea in someone's head?

Issue moved to dotnet/csharplang #129 via ZenHub

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codingonHP picture codingonHP  路  3Comments

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments

glennblock picture glennblock  路  3Comments

marler8997 picture marler8997  路  3Comments