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

Suggestion: Allow ignored lambda parameters to have the name _ #2397

Closed
erik-kallen opened this issue Apr 30, 2015 · 10 comments
Closed

Suggestion: Allow ignored lambda parameters to have the name _ #2397

erik-kallen opened this issue Apr 30, 2015 · 10 comments

Comments

@erik-kallen
Copy link

When writing lambdas you quite often end up not caring about all parameters, but you need names for them anyway. In F# you can use _ as the name for such parameters, and that is also possible in C# but only as long as there is only one such parameters. If there is more than one you need to come up with names (or use names such as _1, _2 etc).

My suggestion is to allow multiple symbols in the same scope be named _. Of couse, when doing this you can't use any of the symbols with that name, but it will enable you to write things like MyMethod((a, _, _) => a + 1) which is sometimes convenient (especially when using mocking libraries).

@AdamSpeight2008
Copy link
Contributor

Doesn't work for vb.net _ is the line continuation character,

@xbelt
Copy link

xbelt commented Apr 30, 2015

Well it does not necessarly need to be the underscore. I could also imagine somethind like:
(a, ?,?) => a + 1
or simply
(a, ,) => a + 1
Personally I find this a nice idea

@HaloFour
Copy link

Check out #20 and #2145.

Since the pattern matching spec is very likely to include a wildcard, which thus far as been *, I'd recommend reusing that character in these cases.

Note that _ is a legal identifier today. Changing the meaning would break code. The C# team is a lot less willing to do this than the Java team (where this very transition is already underway).

@AdamSpeight2008
Copy link
Contributor

@xbelt I also like the look of the second option (a, , ), as I think of that form of syntax as a tuple. It is a tuple<parameter,parameter,parameter>

@erik-kallen
Copy link
Author

My suggestion is for C# although it might be useful for vb too but I don't use that language,

Using the underscore for this would not be a breaking change in C# as long as the rule is specified to be that "if more than one symbol in a scope has the name _, an error is not issued but none of those symbols can be accessed". It is not a breaking change because such program is illegal today.

@HaloFour
Copy link

Maybe, but it would be silly to have multiple wildcard operators in different contexts. If pattern matching is going to use * then it doesn't make sense for parameter ignoring to use _, especially since _ is a perfectly legal C# identifier.

@aluanhaddad
Copy link

@HaloFour Yes, the syntax should definitely align with that of pattern matching.

@gulshan
Copy link

gulshan commented Dec 28, 2016

This is probably done now with _ discard.

@alrz
Copy link
Contributor

alrz commented Jan 1, 2017

Not just yet. AFAIK it only works on patterns, out var, deconstructors and simple assignments.

@jcouv jcouv self-assigned this May 29, 2017
@jcouv jcouv changed the title Suggestion: Allow ignored parameters to have the name _ Suggestion: Allow ignored lambda parameters to have the name _ Jul 6, 2017
@jcouv
Copy link
Member

jcouv commented Aug 31, 2017

I'll go ahead and close this language proposal in favor of the one in the csharplang repo dotnet/csharplang#111
Thanks

@jcouv jcouv closed this as completed Aug 31, 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