gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:49:29 25 4
gpt4 key购买 nike

我想在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

最佳答案

好吧,由于有很多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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com