Replies: 18 comments 8 replies
-
Is this something that should be related to Bestest Betterness #98? |
Beta Was this translation helpful? Give feedback.
-
It depends on the shape of the proposed solution/specification. Absent a proposed solution, I would say no. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
How about the following as a starting point:
To:
To:
(my additions in italics). This seems trivially simple. So what have I missed? 😁 |
Beta Was this translation helpful? Give feedback.
-
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). |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
Also, when there is exactly one method in the method group but with a |
Beta Was this translation helpful? Give feedback.
-
This keeps coming back. What's the smallest change we can make to the spec that would allow this to resolve? |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
As a proof of concept, here's the minimum changes for such program to successfully compile: |
Beta Was this translation helpful? Give feedback.
-
Would this help with this issue? If so, then this would be a really helpful improvement! |
Beta Was this translation helpful? Give feedback.
-
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;
} |
Beta Was this translation helpful? Give feedback.
-
Is this change happening? There seems to be agreement that it would be a good thing to have. |
Beta Was this translation helpful? Give feedback.
-
This is likely addressed by "lambda natural type" feature (@cston?) |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
@jnm2 commented on Mon Jan 30 2017
Please allow the following. I see no possible ambiguity in this scenario. The
IsEven
andTest
method 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?
Beta Was this translation helpful? Give feedback.
All reactions