33

I tried naming a lambda parameter _, e.g. (a cut down version):

Consumer<Object> c = _ -> {};

as I wanted to signify that a parameter was being ignored, but I got the following compiler error:

use of '_' as an identifier is forbidden for lambda parameters

This was a surprise for me. Interestingly, two underscores is OK:

Consumer<Object> c = __ -> {}; // no compile error

So it's not the underscore character in general, but a single one.

Why is the single-underscore name specifically forbidden?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 6
    Just remember: just because two underscores is not forbidden, doesn't make it a good idea. The goal is not to outwit the compiler, it is to write code that the poor folks who have *read* your code will never have to spend a second wondering "what does this code do." – Brian Goetz Dec 31 '15 at 17:16
  • 3
    @BrianGoetz I think the idea is good. The use case that lead to my discovery of this (and genuine surprise) was coding a consumer that didn't use its parameter (to pass to an existing API). I generally use single letters for lambda parameter names, so I thought I'd follow spock's lead (of using an underscore as a wildcard) to convey "ignore". I ended up just using "ignore" as the name. Trying a double underscore was not an attempt at circumvention; it was a test to confirm that only a single underscore was illegal (rather than the underscore character in general). Happy New Year :) – Bohemian Jan 01 '16 at 03:44
  • 2
    @holger this is not a dupe. The other question doesn't ask *why*, this does. – Bohemian Jan 01 '16 at 08:44
  • 2
    @Bohemian: but the *answer* explains why. This is exactly what “This question has an answer here” means. Not that the question is identical. There is even a link to Brian Goetz’ post (but in a comment though)… – Holger Jan 01 '16 at 21:09
  • Possible duplicate of [\_ (underscore) is a reserved keyword](http://stackoverflow.com/questions/23523946/underscore-is-a-reserved-keyword) – Tunaki Oct 26 '16 at 09:31
  • The question does ask _why_ they're getting a warning (they're looking for an explanation). The issue with _why_ on design questions is that it's always unconfomtable to settle for an answer. But the JLS, as shown on the linked answer, is quite clear, and even explains the reasoning behind the restriction. – Tunaki Oct 26 '16 at 09:36
  • Why do this? It seems stylistically repellant to me. – ncmathsadist Nov 11 '19 at 18:01
  • 1
    @ncmathsadist well one very good reason to use the underscore is that [groovy](http://groovy-lang.org/), which is widely used, uses the underscore as a placeholder for "anything" (nicknamed the *wunderbar*) in its closures (ie lambdas) and thus is a precedent. – Bohemian Nov 11 '19 at 22:54
  • @ncmathsadist ??? Single underscore as unused parameter name seems appropriate to me – Enerccio Oct 31 '21 at 10:00

1 Answers1

35

The reason is expressed in this post from Brian Goetz himself:

We are "reclaiming" the syntactic real estate of "_" from the space of identifiers for use in future language features. However, because there are existing programs that might use it, it is a warning for identifiers that occur in existing syntactic positions for 8, and an error for lambda formals (since there is no existing code with lambdas.)

Oli
  • 2,507
  • 1
  • 11
  • 23
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This happened a short time after an other new academic funded programming language (Scala) used the underscore for special purposes. – Joop Eggen Dec 08 '19 at 22:28
  • 1
    @Joop groovy, like scala, also uses it for "any number/type of parameter(s)" – Bohemian Dec 08 '19 at 22:30
  • mail is here, https://mail.openjdk.org/pipermail/lambda-dev/2013-July/010670.html – djy Jan 20 '23 at 09:37