Why doesn't Java 8's Predicate extend Function

前端 未结 4 1010
慢半拍i
慢半拍i 2021-02-03 19:14

If I wrote the Predicate interface, I\'d want to encode in the interface the fact that it\'s just a function that returns a primitive boolean, like thi

4条回答
  •  星月不相逢
    2021-02-03 19:49

    In my opinion Function is just the definition of a generic function. If all FunctionalInterfaces would implement Function, the only abstract method would have to be named apply(). In the context of a concrete FunctionalInterface like FilterFile, the abstract method boolean accept(File pathname) is a much better name then Boolean apply(File).

    The annotation @FunctionalInterface already marks an interface as being intended to be usable as a FunctionalInterface. There is no benefit having them all implement a base interface other then processing them in a generic way. I do not see when you would not care about the semantics of a FunctionalInterface in advance to make them available to call apply for them all.

提交回复
热议问题