• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 15741
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is such an example from Scala:

 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a good explanation. Thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2966
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic