Repository navigation
Replies: 18 comments 8 replies
|
Is this something that should be related to Bestest Betterness #98? |
|
This is such an annoying feature of C#. Having it infer the type parameters when the method group only consists of one method would be a really useful addition to the language. |
|
This does seem like a major flaw that needs to be rectified, especially for those of us using first-class functions everywhere -- it's a real pain point. The fact that this invisible thing called a 'method group' (which no coder understands unless they know how the compiler works) gets in the way of treating methods as first-class functions is crazy. Even if there are multiple methods in the group. "Method group" shouldn't even be in a C# programmer's lexicon. |
|
I just got bitten by this. Thought I was being really clever and created a t4 template to generate helpers for partial function application only to find I couldn't pass "method groups" to them. https://gist.github.com/bradphelan/ca92cd8e8a7b619712799474a27f60e6 |
|
I hear people asking for a solution to this. Has anyone a proposed solution (i.e. a modification to the language specification) that is implementable? |
|
How about the following as a starting point:
To:
To:
(my additions in italics). This seems trivially simple. So what have I missed? π |
|
That doesn't quite work. The type of a method in a method group isn't known until after type inference (because it could be a generic method). |
|
Also, the number of methods in a method group isn't a defined concept. When there is no instance method but there is an extension method, what is the number of methods in the method group? |
|
Also, when there is exactly one method in the method group but with a |
|
This keeps coming back. What's the smallest change we can make to the spec that would allow this to resolve? |
|
I think the root of the problem is where for type inference on the method group, we need to know what the exact delegate type it is going to. We could infer |
|
As a proof of concept, here's the minimum changes for such program to successfully compile: |
|
Would this help with this issue? If so, then this would be a really helpful improvement! |
|
I recently hit something similar to this but I'm not sure that would be the same issue as it won't work with a lambda expression either, interface I {
Task<object> MethodAsync(object x);
}
static class C {
static void Test()
{
Create((I v) => v.MethodAsync); // error
Create((I v) => x => v.MethodAsync(x)); // error
Create<object, object, I>(v => v.MethodAsync); // ok
}
static Func<T, U> Create<T, U, V>(Func<V, Func<T, Task<U>>> factory) => null;
} |
|
Is this change happening? There seems to be agreement that it would be a good thing to have. |
|
This is likely addressed by "lambda natural type" feature (@cston?) |
|
I drafted a spec change to solve this, along with various method group improvements. Here's the relevant part:
That is the mechanism that allowed explicitly-typed lambdas to contribute to type inference: So this change should allow a method group with a unique signature to work: |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
@jnm2 commented on Mon Jan 30 2017
Please allow the following. I see no possible ambiguity in this scenario. The
IsEvenandTestmethod groups have only one overload which fits the delegate signature.Expected Behavior:
Infers
Test<int>(IsEven)and compilesActual Behavior:
CS0411 The type arguments for method 'Program.Test(Func<T, bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
@jaredpar commented on Mon Jan 30 2017
Believe this is by design but will let @gafter comment on exactly how.
@gafter commented on Mon Jan 30 2017
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 reactions