I recently came across this when trying to provide common functionality to a sub-class of a functional interface. Apparently you cannot do it the way I thought (atleast for Java 8):
@FunctionalInterface
public interface Base<T extends Base<T>> {
void doit();
default T someDefault() {
return () -> {}; //incompatible types: T is not a functional interface
}
}
Is there a way to do this? Or was it intentionally disallowed? Maybe an abuse of default methods?
Maybe a use-case example would be a good idea:
@FunctionalInterface
public interface Processor<I, T extends Processor<I, T>> {
T process(I input);
default T requireNonNull(I i) {
return (I input) -> {
Objects.requireNonNull(i);
process(input);
};
}
}
@FunctionalInterface
public interface Parser<T> extends Processor<String, T> {
@Override
default T process(String s) {
return parse(s);
}
T parse(String s);
}