2

I'm trying to find a name similar to what i,j are for loops, or x,y is for coordinates etc.

I have a code like this:

DbSetup.setupCommon(x -> HibernateHelper.addResource(SpecificEntityHelper.HIBERNATE_RESOURCE, schemaName));

In this case x is not a required variable name and I found it is used in few places in code base of our project, so probably is Of course in this specific case I can not use a static reference to the function HibernateHelper::addResource and it sounds like there is no other way to not to have a name of the variable at all.

OMax
  • 539
  • 3
  • 8
  • 1
    You can see from [this](https://stackoverflow.com/questions/23523946/underscore-is-a-reserved-keyword) that `_` was intended to do this, but it appears to have been forgotten. A comment there suggests `$`. – Amadan Mar 18 '21 at 05:48
  • It hasn't been "forgotten". You can see from [JLS 3.9](https://docs.oracle.com/javase/specs/jls/se16/html/jls-3.html#jls-3.9) that it is reserved for **possible** future use. Frankly, any single character identifier will work as well as any other one. – Stephen C Mar 18 '21 at 06:34
  • @StephenC: The link I posted has a link to an email that I am almost certain is the origin of JLS 3.9, and gives explicitly just one example of the those possible future uses: *things it might be used for include things like "I don't want to give this variable a name"* (pretty much exactly what this question asks about). AFAIK `_` remained "reserved for possible future uses" for almost eight years now, with none of them being implemented, so... I think "forgotten" might be an adequate description of the situation. – Amadan Mar 18 '21 at 09:59
  • 1
    An alternative reading is that they thought better of that particular idea. Reading the Brian Goetz email, the take away is that they were reclaiming `_` ... not that they had specific plans to use it. Note given that they decided to reclaim `_`, it was best that they did it ASAP ... to stop it spreading through everyone's codebase (like `$` has done in javascript.) The longer they left it, the harder it would have been to reclaim. – Stephen C Mar 18 '21 at 13:32

1 Answers1

2

Currently, Java syntax doesn't provide a way to have no identifier at all if the argument is to be ignored.

A single _ may serve that purpose in the future, but as of Java 16 _ is just a keyword that is "reserved for possible future use in parameter declarations."; see JLS 3.9.)

It is inadvisable to use $ because all identifiers that contain $ are reserved for use by source code generators or for legacy purposes; see JLS 3.8

Also, there isn't an established conventional name for a dummy argument to a lambda expression.


My advice would be to just use a single letter identifier; e.g. x. A lambda expression will typically be small enough that you can easily see that (say) x is not used in the expression.

Alternatively, you could pick a name like unused or dummy or ignore to flag your intent to not use the value.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216