Say I have the following interface:
@FunctionalInterface
public interface First {
int fun(int a);
}
and also
@FunctionalInterface
public interface Second extends First {
default int fun(int a) { fun(a, a); }
int fun(int a, int b);
}
Then if I have a method somewhere that takes a First
I can do, for example:
methodThatTakeFirst(val -> val + 1);
But I also want to be able to pass a Second
, like:
methodThatTakeFirst((v1, v2) -> v2 * v2);
However this only works if I cast the lambda like this:
methodThatTakeFirst((Second) (v1, v2) -> v2 * v2);
My question is: is there a way to design this pattern without having to cast the lambdas to the subinterface? Or what would be the most elegant way to handle this scenarios?