drofwarcs
03/01/2019, 8:51 PMfun doBar(callBack: (String, Array<Int>) -> Unit)
. That gets exposed to Java as
doBar(Function2<? super String, ? super Integer[], Unit> callback)
. When ever we try to create a anonymous class of Function2 we get doBar(Function2<? super String, ? super Integer[], Unit> callback) cannot be applied to (anonymous Function2< String, Integer[], Unit>)
. Is there another way to call a kotlin function interface from Java?Czar
03/01/2019, 8:57 PMdoBar((s, integers) -> {
System.out.println(s);
System.out.println(integers.length);
return null;
});
If it's a lambda why bother creating anonymous class of Function2? You should take advantage of Java SAM conversion and use Java lambda.doBar(new Function2<>() {
@Override
public Unit invoke(String s, Integer[] integers) {
System.out.println(s);
System.out.println(integers.length);
return null;
}
});
drofwarcs
03/01/2019, 9:33 PMFunction2<? super String, ? super Integer[], Unit> is not a functional interface
Czar
03/01/2019, 9:38 PMdoBar
as
fun doBar(callback: (String, Array<Int>) -> Unit) {
callback("vasya", arrayOf(1,2,3))
}
Then the java code above does indeed print:
> Task :TstJ.main()
vasya
3
drofwarcs
03/01/2019, 9:45 PM