• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How is return type of Scala lambda derived when we don't have functional interface unlike Java?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java the return type of lambda is determined by the functional interface.

In Scala, functional interface is not required for scala functional programming . How is return type derived in Scala lambda?

Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand how it happened in Java (derived on the basis of return type of functional interface method )  but not in case of Scala
 
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, have you looked at the signatures of the methods that accept a function in Scala?

For instance, IterableOnceOps contains a method find(p: (A) => Boolean): Option[A], that takes a predicate of the form (A) => Boolean. So obviously when you pass a lambda expression to the find() method, it must return a (A) => Boolean, which is Scala's equivalent of Java's Predicate<T> interface.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java we can explicitly place a return statement (although it is not required in most cases). I will check whether it is possible in Scala too.
 
Marshal
Posts: 79060
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a great semantic difference between an expression and a return statement.
 
Ranch Hand
Posts: 338
Scala Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Monica,

Scala do have Functional Interfaces like Java 8 in form of Function0, Function1 until Function22.. which take 0, 1..22 parameters respectively.
The return type of the Scala Lambda can be one of these 22 Functions.
They are not Functional Interfaces but they are traits something similar to interfaces.
These 22 traits also have a single abstract method(apply with equivalent number of parameters i.e. 0,1,...22)  similar to functional interfaces in Java 8.
They can be found in the package named scala.

Hope this gives a clear picture of how scala lambdas work.

Regards,
-Pankaj.

 
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic