If I create a Functional interface:
@FunctionalInterface
public class Consumer2<T1, T2> {
void accept(T1 t1, T2 t2);
default Consumer1<T2> curry(T1 t1) {
return (t2) -> accept(t1, t2);
}
}
Now, if I have a class:
public class MyClass {
public void printStrings(String a, String b) {
System.out.println(a + ": " + b);
}
}
MyClass myClass = new MyClass();
Now, if I want to use my functional interface, I can:
Consumer2<String, String> printString = myClass::printStrings;
printString.curry("hello").accept("world");
But I can't do something like:
myClass::printStrings.curry("hello").accept("world");
which makes sense, because Java has no way of knowing that myClass::printStrings can be applied to the functional interface Consumer2. To do this, I have created a utility class:
public class F {
public static <T1, T2> Consumer2<T1, T2> c2(Consumer2<T1, T2> fn) {
return fn;
}
}
Then I can:
F.c2(myClass::printStrings).curry("hello").accept("world");
Even, this will work:
((Consumer2<String, String>)myClass::printStrings).curry("hello").accept("world");
As long as there is some way for Java 8 to understand that functional type in this case. So, the question is, what is the best way to do it, while possibly avoiding the boilerplate?
class Consumer2is supposed to beinterface Consumer2…