2016-04-10 33 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())