【问题标题】:Kotlin type inference compile error when using Akka java API使用 Akka java API 时 Kotlin 类型推断编译错误
【发布时间】:2016-04-10 04:48:26
【问题描述】:

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

【问题讨论】:

    标签: java akka type-inference kotlin


    【解决方案1】:

    好吧,由于有很多 Scala 的东西和&lt;no name provided&gt;,错误消息可能有点不清楚,但它清楚地定义了错误的重点:您的函数应该接受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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多