2016-04-10 35 views
1

我想在Kotlin程序中使用Akka java API。当我想设置onComplete回调的阿卡Future,我遇到科特林编译器错误,而Java的同等工作很好:使用Akka java API时Kotlin类型推理编译错误

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()); 

的科特林编译器错误:

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

回答

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())