我想在Kotlin程序中使用Akka java API。当我想为akka onComplete
设置Future
回调时,我会遇到Kotlin编译器错误,而java等效工作很好:
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());
Kotlin编译器错误:
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。
答案 0 :(得分:2)
好吧,由于很多Scala和<no name provided>
,错误信息可能有点不清楚,但它清楚地定义了错误点:你的函数应该接受Any
,而不是Object
。以下代码编译没有任何问题:
val future: Future<Any> = ask(sender, MyActor.Greeting("Saeed"), 5000)
future.onComplete(object : OnComplete<Any?>() {
override fun onComplete(failure: Throwable?, success: Any?) {
throw UnsupportedOperationException()
}
}, context.dispatcher())