我想要做這樣的事情。斯卡拉:在方法多return語句返回未來[布爾]
def foo(s : String): Future[Boolean] = Future {
val a = someLongRunningMethod
if(!a)
return false or throw some exception // what should i return
//do some more thing
val b= someMoreLongRunningMethod
if(b)
return true
return false
}
但無法使用帶布爾值的返回值。我有類型不匹配錯誤。
Error:(32, 12) type mismatch;
found : Boolean(false)
required: scala.concurrent.Future[Boolean]
return false
我是斯卡拉新手。我正在使用foo方法。我不知道這是否是使用它的最佳方式。請建議我應該如何實現它?
val r = foo("Nishant")
r.onComplete {
case Success(result) => {
//Do something with my list
println("success: " + result)
}
case Failure(exception) => {
//Do something with my error
println("failure")
}
}
val re = Await.result(r, 10 second)
您需要首先看了一則關於'Future'類型是如何工作的教程(順便說一句使用這樣的回報是不是斯卡拉idomatic) – cchantep
http://tpolecat.github.io/2014/05/09/return html的 – michaJlS
我讀了一些教程,但個個都與單一的if/else塊的例子。你能推薦一些很好的教程嗎 –