Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Is the => symbol not mandatory for Scala lambda expression as in this statement?  RSS feed

 
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In scala for lambda expressions => symbol is used. I came across a code where lambda expression is used but there is no => symbol. Is this symbol not mandatory?



In the above code I am talking about this part:



I thought we have to use the below syntax:



Do we not require the => symbol?

Thanks
 
Saloon Keeper
Posts: 3255
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just one of those famous shorthands of Scala, but one that I never liked. It may save a few characters, but I prefer the full version (much more clear).
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks . So I can ignore this syntax and better use the arrow syntax.
 
Ranch Foreman
Posts: 3251
19
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can choose not to ever use it in your own code.  If it occurs in code written by others though, it's probably more useful to understand it, rather than ignore it.

This syntax is referred to as placeholder syntax for function arguments.  When declaring a function literal, you can choose to omit the argument list and the =>.  Instead you use _ for each argument.  The first use of _ refers to the first argument, the second refers to the second argument, etc.
 
Sheriff
Posts: 13478
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Mike. Also, if it's idiomatic, then I would prefer it over the longer form.
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!