-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Instance Method Delegate (Method References) #5444
Comments
That's actually rather clever. |
👍 |
👍 |
👍 |
edit: (there is no method
could be currently written as this:
or:
But the latter can also be written as an expression tree:
So:
those are likely expressions, not "Instance Method Delegates" |
@bbarry Fixed. |
I've edited the previous post; I am not sure what I didn't read?
When would referencing a setter this way be applicable? The current proposal is ambiguous in some cases, would:
represent:
or
|
@bbarry I don't see how that case is ambiguous. Why would the compiler even consider the second Also, your second example would simply be a compiler error since there is no The one way it could be ambiguous is if a class has an instance method and a static method of the same name where the static method accepts an instance of the class as the first parameter, assuming that the |
yeah I meant |
This is a great idea. It could probably be implemented using open-instance delegates. |
If possible I would also like to see something like this working:
or alternatively using the
This is rather important to me as I am using reflection very often. |
@Unknown6656 That's a separate feature, see #1653. |
Great idea |
I'd prefer a type-safe reflection mechanism to get unbound field and property references, but since that seems unlikely, this is an interesting alternative. I like the proposal, but not expecting the compiler to resolve the ambiguity between get and set through context. I'd prefer to see a syntax, more like:
|
With regards to syntax: With regards to properties: |
@miniBill that's a sensible idea but there's no ambiguity that needs to be eliminated because the signatures of a getter and the setter are sufficiently distinct. |
@aluanhaddad That's true, but for |
Moved to dotnet/csharplang#84 |
Currently you can create delegates from static methods and non-static methods of a specific instance:
But what if you want to create a direct delegate from a non-static method without a specific instance? Currently this is supported by
Delegate.CreateDelegate
but it's too verbose as you must specify the exact delegate type and pass theMethodInfo
using reflection. I propose the instance method delegates (aka method references in Java):The syntax used here should be able to expose method accessors for properties:
This can also cover the #3561:
Note that these members are properties rather than methods but since they are internally implemented as methods so it makes sense to create delegates from them. For the fact that each property may consist of two methods (getter and setter) the right choice should be inferred from usage.
In these specific cases where the target type can be inferred, an alternative would be
.Select(::Employee)
— a syntax closer to what have proposed in the aforementioned issue.EDIT: there should be a special translation for LINQ, so:
to avoid creating double-invocation delegates.
The text was updated successfully, but these errors were encountered: