This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java 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: 70
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: 70
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: 59422
187
  • 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: 8990
168
  • Likes 1
  • 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: 59422
187
  • 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: 8990
168
  • 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: 59422
187
  • 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: 59422
187
  • 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: 59422
187
  • 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.
 
Saloon Keeper
Posts: 1988
265
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: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.


This agrees with what is mentioned in JEP 302: Lambda Leftovers:

Treatment of underscores
In many languages, it is common to use an underscore (_) to denote an unnamed lambda parameter (and similarly for method and exception parameters):

    BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);

This allows stronger static checking of unused arguments, and also allows multiple arguments to be marked as unused. However, because underscore was a valid identifier as of Java 8, compatibility required us to take a more indirect path to getting to where underscore could serve this role in Java. Phase 1 was forbidding underscore as a lambda formal parameter name in Java 8 (this had no compatibility consequence, since lambdas did not exist previously) and a warning was issued for using underscore as an identifier in other places. Phase 2 came in Java 9, when this warning became an error. We are now free to complete the planned rehabilitation of underscore to indicate an unused lambda, method, or catch formal parameter.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!