This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 60168
188
  • 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: 9255
177
  • 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: 60168
188
  • 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: 9255
177
  • 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: 60168
188
  • 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: 60168
188
  • 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: 60168
188
  • 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.
 
Bartender
Posts: 2038
273
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!