Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Lamda parameter with _(underscore) variable name in Java 8/9  RSS feed

 
Ranch Hand
Posts: 59
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Authors,

I've written a tiny program that implements simple lambda. I get an error as shown in below.


As described in JDK 8, it is true.
In Java 8, The lambda identifier can not be declared with _(underscore) identifier such as : _ -> 0

For e.g,



In JDK 9, use of _ (underscore) as an identifier causes an compile-time error.
In the following line   //    t(_ -> 0);  //line 1, it will be occurred an error while it is uncommented.

The highlight of the above code such that this rule is applied only to '_', but not for other valid identifiers such as '$'.

The question is to know here, "would the underscore be in reserved collection or not in JDK 8 or further?


Thank you for your reply in advance.







 
Noorul Hameed
Ranch Hand
Posts: 59
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:  In JDK 9, use of _ (underscore) as an identifier causes an compile-time error.
In the following line   //    t(_ -> 0);  //line 1, it will be occurred an error while it is uncommented.



Typo error is there: please consider line 2 instead of line 1.

thanks
 
Marshal
Posts: 58829
179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The underscore alone is a keyword, as you will find here. It is never used; it is a keyword to prevent it from being used as an identifier. Up to Java8 people used _ as an identifier, which was regarded as confusing code, so that is now prohibited. Try compiling your code with Java8 or older. The use of _ alone to the left of -> was prohibited in λs in Java8.
 
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reason is that in many functional languages, underscore is used to indicate that you don't care about the value of a certain parameter because you're not going to use it. Maybe the Java designers wanted to keep this option open until they decided what they really wanted to do, so they prevented people from using it as an identifier so they don't have to worry about backwards compatibility.

Maybe in the future they will allow the use of underscore as a parameter name in lambda expressions, while prohibiting their use in lambda expression bodies, thereby enforcing the convention that underscores indicate the parameter is not used.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe; that might be what is hinted at in the Java8 JLS link I posted.
 
Stephan van Hulst
Saloon Keeper
Posts: 8734
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't find any hints of the kind.
 
author
Ranch Hand
Posts: 200
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually didn't know about underscore as I've never tried to use it as an identifier!

I personally think it's a really bad idea to use _ as an identifier... I'd recommend avoiding that.

 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I couldn't find any hints of the kind.

That JLS section wrote:The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics.

Just a hint.
 
Ranch Hand
Posts: 64
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe; that might be what is hinted at in the Java8 JLS link I posted.


The link you've posted is Java10 JLS not Java8 JLS

Stephan van Hulst wrote:I couldn't find any hints of the kind.


The hint is in the "Identifiers paragraph" here with very small font ;)
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ahmed Ibrahim wrote:. . . The link you've posted is Java10 JLS not Java8 JLS . . .

Good catch , but the second link I posted, this one, is to the Java8 JLS. The Java9/10 equivalent pages include this:-

JLS §15.27.1 wrote: In Java SE 8, the use of _ as the name of a lambda parameter was forbidden, and its use discouraged as the name for other kinds of variable (§4.12.3). As of Java SE 9, _ is a keyword (§3.9) so it cannot be used as a variable name in any context.

 
Ahmed Ibrahim
Ranch Hand
Posts: 64
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
the second link I posted, this one, is to the Java8 JLS


yes, i found the hint you've mentioned in the second link "The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics."

but with very small font
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but the use of _ might be permitted again in Java15; who knows. They might find some potential use for it to fulfil the bit about special syntax.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!