• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Monica Shiralkar
Ranch Foreman
Posts: 1112
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
 
Piet Souris
Master Rancher
Posts: 3189
119
  • 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 Foreman
Posts: 1112
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.
 
Mike Simmons
Ranch Hand
Posts: 3218
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.
 
Junilu Lacar
Marshal
Posts: 13447
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 Foreman
Posts: 1112
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Popeye has his spinach. I have this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!