Java 8 : Lambda Function and Generic Wildcards

左心房为你撑大大i 提交于 2021-02-18 22:10:27

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/30818997/java-8-lambda-function-and-generic-wildcards

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!