Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 62
  • 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: 12154
258
  • 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: 69847
    278
    • 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: 69847
    278
    • 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: 12154
    258
    • 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: 69847
    278
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
     
    This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic