java - Functional Interface Inheritance Quirk -


i have custom interface i've been using time looks this:

public interface function<t, r> {     r call(t input); } 

i'd retrofit interface both java's function guava's function, while keeping functionalinterface. thought had perfect arrangement:

@functionalinterface public interface function<t, r> extends         java.util.function.function<t, r>,         com.google.common.base.function<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

both superinterfaces declare same apply() method, has been implemented in interface, leaving abstract call() method. strangely, won't compile, telling me

invalid '@functionalinterface' annotation; function<t,r> not functional interface

stranger still, following variations compile fine:

@functionalinterface public interface function<t, r> extends         java.util.function.function<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

@functionalinterface public interface function<t, r> extends         com.google.common.base.function<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

public interface function<t, r> extends         java.util.function.function<t, r>,         com.google.common.base.function<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

@functionalinterface public interface function<t, r> extends         java.util.function.function<t, r>,         com.google.common.base.function<t, r> {      @override     r apply(t input); } 

is there reason first version won't compile?

as stated in comments, compiles fine oracle compiler. eclipse bug.

awaiting bug fix, remove annotation @functionalinterface (your 3rd variation):

public interface function<t, r>                                 extends                                     java.util.function.function<t, r>,                                     com.google.common.base.function<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

the major inconvenient of solution eclipse compiler bug prevent using function lambda target type.


if want keep @functionalinterface on function, (ugly) workaround might introduce intermediate interface:

public interface adapterfunction<t, r>                                       extends                                           java.util.function.function<t, r>,                                           com.google.common.base.function<t, r> {     @override     default r apply(t input) {         return null;     } } 

and let function extends adapterfunction:

@functionalinterface public interface function<t, r>                                 extends                                     adapterfunction<t, r> {      r call(t input);      @override     default r apply(t input) {         return call(input);     } } 

in case, function valid target type eclipse too:

function<string, object> function = st -> st.tostring(); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -