I have a JAVA class:
public class MyDispatcher {
public <T> void post(Supplier<T> action) {
var result = action.get();
System.out.println("post(Supplier<T> action), result=" + result);
}
public void post(Runnable action) {
System.out.println("post(Runnable action)");
action.run();
}
public <T> void post2(Supplier<T> action) {
var result = action.get();
System.out.println("post2(Supplier<T> action), result=" + result);
}
public interface MyCallable<T> {
T call();
}
public <T> void post2(MyCallable<CompletionStage<T>> action) {
action.call().thenAccept(result -> {
System.out.println("post2(MyCallable<CompletionStage<T>> action), result=" + result);
});
}
}
When called from JAVA, the function overload works correctly:
var d = new MyDispatcher();
d.post(() -> {});
d.post(() -> 222);
d.post2(() -> 333);
d.post2(() -> CompletableFuture.completedStage(444));
When called from kotlin, lambdas can’t be resolved to the intended methods correctly:
val d = MyDispatcher()
d.post {}
d.post { 111 } // resolved to post(Runnable) instead of post(Supplier<T>)
// d.post2 { 222 } // won't compile
val l2 = { 222 }
d.post2(l2)
d.post2 { CompletableFuture.completedStage(333) }
Is this a bug in the kotlin compiler?