This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

How to use generic function interface method type parameters in a lambda.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please can you suggested, How to implementation a generic method interface in lambda expression.
 
Saloon Keeper
Posts: 11017
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS says the following:

A lambda expression is compatible in an assignment context, invocation context, or casting context with a target type T if T is a functional interface type (ยง9.8) and the expression is congruent with the function type of the ground target type derived from T.


And:

A lambda expression is congruent with a function type if all of the following are true:

  • The function type has no type parameters.
  • ...

  • https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.27.3

    I made the relevant parts bold. In short, there is no way to write a lambda expression that satisfies a functional interface that contains a method that has type parameters. You can still use method handles though.
     
    Marshal
    Posts: 67036
    255
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Adding discussion to our λs forum.

    You mean, apart from that being the wrong way to declare an interface method? Generic methods are intended for static methods.That isn't the correct way to declare a Comparable; remember that the compareTo() method might be implemented only in a superclass.Now, you can't take a TemperatureFahrenheit and pass it to your method, so what you do is change the generics to what appears here:-That allows you to take something where its superclass implements Comparable. I cannot see how you are going to implement that method like that, but I shall change it a bit:-So you can write λs like this:-Line 279 only works if the Stream elements implement Comparable.

    I suspect you could have written exactly the same λs with your method made generic rather than the interface but I have never tried it.
     
    Campbell Ritchie
    Marshal
    Posts: 67036
    255
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    A few minuste ago, I wrote:. . . I suspect you could have written exactly the same λs with your method made generic rather than the interface but I have never tried it.

    ...and Stephan has shown why my suspicion was incorrect.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11017
    243
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell, I agree with most of your post, except for:

    Campbell Ritchie wrote:Generic methods are intended for static methods.


    This is untrue. It's absolutely fine to use generic type parameters in an instance method. It just doesn't occur very often, and it makes little sense in functional interfaces. There are very valid use cases however. For examples from the standard API, the Stream class is full of them.
     
    Campbell Ritchie
    Marshal
    Posts: 67036
    255
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
     
    All of life is a contant education - Eleanor Roosevelt. Tiny ad:
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!