Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

When introducing lambdas, why did they limit its usage to functional interface implementations?  RSS feed

 
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lambdas were introduced in Java 8. However its usage was kept limited to functional interface implementation unlike some other languages like Scala where there is no such limitation?
 
Ranch Hand
Posts: 530
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give some examples which Lambda expressions should be used on non-functional interfaces?
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Could you give some examples which Lambda expressions should be used on non-functional interfaces?



I am not talking about non functional interfaces. I am saying the usage of lambda expressions is limited to interfaces in Java 8.  
 
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Could you give some examples which Lambda expressions should be used on non-functional interfaces?



I am not talking about non functional interfaces. I am saying the usage of lambda expressions is limited to interfaces in Java 8.  



First thing you have to understand... Java and Scala are two different languages which happen to run on the same environment. Scala, a language designed to embrace functional programming(functions), tends to rely on higher level functional abstractions while Java, a language designed around objects, embraces objects. Its really just a design choice.
 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't Scala lambdas just based off FunctionX traits?
I'm not sure that's massively different to Java's various interfaces.
 
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The internal structure of an object created by a lambda expression is incompatible with the internal structure of regular objects. Consider the following code:

Here's the decompiled bytecode:

You can see that the anonymous() method creates an instance of the anonymous Example$1 class, and then calls invokespecial on it to call the constructor.

In the case of a lambda expression, the method just returns the result of an InvokeDynamic call. InvokeDynamic returns something similar to a MethodHandle that is linked to a CallSite that is wrapped around a method, in this case the static lambda$lambda$0() method. If you don't understand it, don't worry. It's pretty magic, and Java doesn't really have anything in the language that uses InvokeDynamic, other than lambda expressions and default methods.

So why can't InvokeDynamic be used for abstract classes? Well, an abstract class is more than just a method handle. An abstract class may have fields, initializers and constructors. These are run with the invokespecial call that you see in the bytecode for anonymous().

One could ask, why then can't we use a lambda expression for abstract classes that don't have fields, don't have initializers and don't have constructors? Well, if an abstract class has no fields, initializers or constructors, then what is the point of it? You can just use an interface.
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Why does this problem of internal structure incompatibility in java objects not arise in case of some other languages like Scala.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I propose you first write an example in Scala that uses a lambda expression to implement a type that is not an interface. Then we can discuss that example.
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is such an example from Scala:

 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That example translates to a Function1 trait.
And traits are the closest thing in Scala to a Java interface.
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means that in whichever programming language there should be something (whether functional interface, function 1 trait or other), where only 1 method is involved with the functionality which we want to give to the lambda expression function. That it the reason it requires a functional interface or a function 1 trait etc.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:whichever programming language [...] where only 1 method is involved


No. Methods are exclusive to object oriented programming languages. Purely functional languages don't need functional interfaces because they have functions built into the language as first class citizens. In Haskell for instance, EVERYTHING is a function. In those languages you can view a constant as a function that has no parameters and returns itself.

That it the reason it requires a functional interface or a function 1 trait etc.


In Scala, it's not just the Function1 trait, it's ANY of the built-in scala.FunctionX traits, where X is an integer specifying the number of parameters that the function has.
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good explanation. Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 1095
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After practicing a simple program using lambda, my understand is that there is a function which does something and may return something. Now the declaration of this function will be in some functional interface. Also,  there will be a method which which will be required to apply this function ( make use of this function in whichever way e. g applying it on some data). That's all we may need to know about using lambda expressions in Java.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful how you use words that have a specific meaning.

You don't declare functions. You declare types, methods or variables.

You define a function either by writing a (possibly anonymous) class that implements a functional interface, or with a lambda expression.


In the above code, we declared a variable multiply which holds a reference to a function that is defined as a * b.

The parameter and return types of the function that is referenced by multiply are determined by the signature and return type of the apply() method that is declared in the BinaryOperator interface.
 
I am Arthur, King of the Britons. And this is a tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!