java - 使用 Akka java API 时 Kotlin 类型推断编译错误

标签 java akka type-inference kotlin

我想在 Kotlin 程序中使用 Akka java API。当我想为 akka Future 设置 onComplete 回调时,我遇到了 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 .

最佳答案

嗯,错误消息可能有点不清楚,因为有很多 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())

关于java - 使用 Akka java API 时 Kotlin 类型推断编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36525910/

相关文章:

haskell - 是否有可能推断出一种类似反射的闭包类型?

c# - 为什么 C# "var"关键字在循环访问 Visio Masters 集合时不起作用?

java - 选择图像时的 NPE

java - 如何在java中使用具有动态参数的mysql存储过程?

persistence - 在Akka的持久化actor中,创建子actor被认为是副作用,还是状态的创建?

actor - 在Akka中,我如何知道在actorOf()注册后何时可以使用actor?

haskell - GADT 的 : Is there a reason why the weakest or strongest type is not chosen

java - 如何建立一个模型来区分关于 Apple (Inc.) 的推文和关于苹果 (fruit) 的推文?

java - 按钮点击监听器

scala - Play 2.1.1 未加载自定义 Akka 调度程序