Java 8 : Lambda Function and Generic Wildcards -


i have following class

class book implement borrowable  {     @override     public string tostring(function<? extends borrowable                             , string> format) {           return format.apply(this);         } } 

this gives me error cannot use "apply" on this(book object).

my current formatter

 function<book, string> regular_format = book -> "name='" + book.name + '\'' +         ", author='" + book.author + '\'' +         ", year=" + book.year; 

i don't want make lambda function of type

function<borrowable, string> 

as lose access members of book not exposed borrowable.

the function<? extends borrowable, string> type means function able accept some type extends borrowable. not mean accepts book. best solution introduce generic parameter borrowable:

public interface borrowable<t> {     public string tostring(function<? super t, string> format); } 

and specify in book:

public class book implements borrowable<book> {     @override     public string tostring(function<? super book, string> format) {         return format.apply(this);     } } 

it's similar how comparable interface works.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -