Example
In this (simplified) example I can create my MyInterface
-object by using a method reference to apply
, but casting directly doesn't work.
@Test
public void testInterfaceCast(){
Function<String, Integer> func = Integer::parseInt;
MyInterface legal = func::apply; // works
MyInterface illegal = func; // error
}
public interface MyInterface extends Function<String, Integer>{}
The second assignment gives the compiler error:
incompatible types: Function<String,Integer> cannot be converted to MyInterface
The question
Can I do some Generics magic, to be able to cast a Function<T, R>
to an Interface?