在下面的代碼段中,Scala的未來[選項[T]]聯合國包裝
trait MyType1; trait MyType2
import scala.concurrent.Promise
val p1 = Promise[Option[MyType1]]()
val p2 = Promise[MyType2]()
我通過在p1和p2到另一個函數,其中我使用一個成功的未來完成的前景。呼叫此功能後,我嘗試讀取Promise中的值:
trait Test {
// get the Future from the promise
val f1 = p1.future
val f2 = p2.future
for {
someF1Elem <- f1
f1Elem <- someF1Elem
f2Elem <- f1Elem
} yield {
// do something with f1Elem and f2Elem
"..."
}
}
當我嘗試編譯此問題時,出現一些編譯器問題。
Error:(52, 19) type mismatch;
found : Option[Nothing]
required: scala.concurrent.Future[?]
flElem <- someF1Elem
^
IntelliJ顯示沒有錯誤或者什麼都沒有,並且類型看起來是一致的。但我不確定編譯器爲什麼不快樂!任何線索?