1
我想在Kotlin程序中使用Akka java API。當我想設置onComplete
回調的阿卡Future
,我遇到科特林編譯器錯誤,而Java的同等工作很好:使用Akka java API時Kotlin類型推理編譯錯誤
val future: Future<Any> = ask(sender, MyActor.Greeting("Saeed"), 5000)
future.onComplete(object : OnComplete<Object>() {
override fun onComplete(failure: Throwable?, success: Object?) {
throw UnsupportedOperationException()
}
}, context.dispatcher())
Java代碼:
Future<Object> future = ask(sender(), new MyActor.Greeting("Saeed"), 5000);
future.onComplete(new OnComplete<Object>() {
public void onComplete(Throwable failure, Object result) {
if (failure != null) {
System.out.println("We got a failure, handle it here");
} else {
System.out.println("result = "+(String) result);
}
}
},context().dispatcher());
的科特林編譯器錯誤:
Error:(47, 24) Kotlin: Type inference failed:
fun <U : kotlin.Any!> onComplete(p0: scala.Function1<scala.util.Try<kotlin.Any!>!, U!>!, p1: scala.concurrent.ExecutionContext!):
kotlin.Unit cannot be applied to (<no name provided>,scala.concurrent.ExecutionContextExecutor!)
Error:(47, 35) Kotlin: Type mismatch: inferred type is <no name provided> but scala.Function1<scala.util.Try<kotlin.Any!>!, scala.runtime.BoxedUnit!>! was expected
我把項目推到github。