• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 1131
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: 3407
149
  • 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: 1131
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: 3297
22
  • 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: 13551
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!