AbrahamDaniel
AbrahamDaniel

Reputation: 569

Java 8 : Lambda Function and Generic Wildcards

I have the following class

class Book implement Borrowable  {
    @Override
    public String toString(Function<? extends Borrowable
                            , String> format) {
          return format.apply(this);    
    }
}

This gives me an error that i cannot use "apply" on this(Book object).

My current formatter is

 Function<Book, String> REGULAR_FORMAT = book -> "name='" + book.name + '\'' +
        ", author='" + book.author + '\'' +
        ", year=" + book.year;

I don't want to make the lambda function of the type

Function<Borrowable, String>

as I would lose access to the members of Book not exposed by Borrowable.

Upvotes: 8

Views: 2890

Answers (2)

You might be looking for Function<? super Book, String>.

A Function<Book, String> is a valid Function<? extends Borrowable, String>, but so is a Function<DVD, String>. Your method (toString) might be called with a Function<DVD, String>, which you can't pass this to because this isn't a DVD!

Change the argument type to Function<? super Book, String>, perhaps.

Upvotes: 5

Tagir Valeev
Tagir Valeev

Reputation: 100199

The Function<? extends Borrowable, String> type means function that able to accept some type which extends Borrowable. It does not mean that it accepts Book. Probably the best solution is to introduce the generic parameter for Borrowable:

public interface Borrowable<T> {
    public String toString(Function<? super T, String> format);
}

And specify it in Book:

public class Book implements Borrowable<Book> {
    @Override
    public String toString(Function<? super Book, String> format) {
        return format.apply(this);
    }
}

It's similar to how the Comparable interface works.

Upvotes: 8

Related Questions