Skip to content
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: Support Signature Redefinition of an Anonymous Method #2675

Closed
ckpearson opened this issue May 12, 2015 · 6 comments
Closed

Proposal: Support Signature Redefinition of an Anonymous Method #2675

ckpearson opened this issue May 12, 2015 · 6 comments

Comments

@ckpearson
Copy link

Currently when providing a delegate, you can substitute a method group, so (types are included for visibility, I'm aware they can be omitted):

DoSomething((int x) => myIntList.Add(x));

can be rewritten as:

DoSomething(myIntList.Add)

This gets a bit tricky if you end up with a signature that has arguments you're not interested in at that point, e.g.

DoSomething((string s, DateTime d, int x) => myIntList.Add(x));

I'd like to be able to do something akin to:

DoSomething((_,_,x) => myIntList.Add);

whereby the signature of (ignore,ignore,int) matches the signature of (int) and it all lines up nicely.

Now, currently the _ character is just practice, and can't be used multiple times, making it possible to do so would likely be a breaking change - I'm sure someone somewhere has actually referenced the _ argument against practice.

I'd propose introducing a way to get around this:

DoSomething((_,_,x`) => myIntList.Add

I've used the backtick character against the argument I want to include in the redefinition, this allows:

  1. Repeated use of _ as this is a redefinition so they can be forcibly made to not be referenceable
  2. Indication that a redefinition is taking place
  3. Indication of which arguments should be lifted and used

These redefined methods could enforce their assumptions by requiring a method group on the RHS, so the following would be invalid:

DoSomething((_,_,x`) => _.SomeProp = x);

You could even introduce some sort of ordering notation to allow for the redefinition of signatures out-of-order, e.g:

DoSomething((_,val`2,key`) => myDict.Add);

OR

DoSomething((_,_val``,key`) => myDict.Add);

This is pretty trivial I realise, but it'd be one of those syntactic nice-to-haves and prevent having to explicitly write the anonymous method just because in this context I'm not interested in an argument.

A real-world example is the XElement.Validate linq extension method:

var messages = new List<ValidationEventArgs>();
element.Validate(someElement, (_,ea) => messages.Add(ea));

The above could be rewritten to be:

element.Validate(someElement, (_,ea`) => messages.Add);

Or a nicer syntax similar to the following could be used.:

element.Validate(someElement, messages.Add^(_,x`));
@svick
Copy link
Contributor

svick commented May 12, 2015

Specific syntax aside, I don't understand what is the goal of all this.

In what way is element.Validate(someElement, (_,ea`) => messages.Add) any better than element.Validate(someElement, (_,ea) => messages.Add(ea))?

@ckpearson
Copy link
Author

It's not directly, which is why I'm mostly spit-balling around the possibilities of something like this.

This may have been better placed as a comment instead of an issue against the proposal thread.

I guess ultimately what I'm asking is if there's a way of having some sort of partial application for anonymous methods to ease the need to explicitly call and pass arguments that aren't needed.

@Joe4evr
Copy link

Joe4evr commented May 12, 2015

Looks like a dupe of #2397 and #20.

@ckpearson
Copy link
Author

👍 #20 looks the closest! Particularly the suggestion from @HaloFour about using * to omit additional arguments in a lambda.

@aluanhaddad
Copy link

@ckpearson
I find the syntax

DoSomething((_,_,x`) => myIntList.Add)

very confusing.
And in the case of

DoSomething((_,_val``,key`) => myDict.Add)

its really confusing, especially if key and val are of the same type.

That said, I would really love, as you mention, for there to be some form of first class support for partial application.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https:/dotnet/csharplang for C# specific issues, https:/dotnet/vblang for VB-specific features, and https:/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants