Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
Ranch Foreman
Posts: 1782
9
  • Mark post as helpful
  • send pies
  • 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 Foreman
Posts: 1782
9
  • Mark post as helpful
  • send pies
  • 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: 12416
269
  • Mark post as helpful
  • send pies
  • 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 Foreman
Posts: 1782
9
  • Mark post as helpful
  • send pies
  • 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: 70612
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a great semantic difference between an expression and a return statement.
 
Ranch Hand
Posts: 337
Scala Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
 
reply
    Bookmark Topic Watch Topic
  • New Topic