使用Akka java API时Kotlintypes推理编译错误
我想在Kotlin程序中使用Akka java API。 当我想为akka设置onComplete
回调Future
,我遇到了Kotlin编译器错误,而java等价的工作很好:
val future: Future = ask(sender, MyActor.Greeting("Saeed"), 5000) future.onComplete(object : OnComplete() { override fun onComplete(failure: Throwable?, success: Object?) { throw UnsupportedOperationException() } }, context.dispatcher())
java代码:
Future future = ask(sender(), new MyActor.Greeting("Saeed"), 5000); future.onComplete(new OnComplete() { 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 onComplete(p0: scala.Function1<scala.util.Try!, U!>!, p1: scala.concurrent.ExecutionContext!): kotlin.Unit cannot be applied to (,scala.concurrent.ExecutionContextExecutor!) Error:(47, 35) Kotlin: Type mismatch: inferred type is but scala.Function1<scala.util.Try!, scala.runtime.BoxedUnit!>! was expected
我把这个项目推给了github 。
那么,错误信息可能有点不清楚,因为很多Scala的东西和
,但它明确定义的错误点:你的函数应该接受一个Any
,而不是一个Object
。 以下代码编译没有任何问题:
val future: Future = ask(sender, MyActor.Greeting("Saeed"), 5000) future.onComplete(object : OnComplete() { override fun onComplete(failure: Throwable?, success: Any?) { throw UnsupportedOperationException() } }, context.dispatcher())