Scala从函数而不是Future [自定义对象]返回List()

时间:2016-11-14 21:48:08

标签: scala

我有下面的代码,哪个方法返回Future [Account],其中Account是案例类对象。以下是代码

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Account (acctId:String, userId:String)
object NewTest extends App{

val account = getAcct
println ("account Fut is " +account)

def getAcct(): Future[Account] = {

 val accountFutOpt = Future {
   //Some(Account("1234","5678"))
   None
 }

(for {
  accountOpt <- accountFutOpt
  account = accountOpt
  if account.isDefined
 }yield account.get).recoverWith {
   case _: NoSuchElementException =>
    //println(s"No Account associated to acctId ")
    Future.failed(new IllegalStateException(s"No account associated to acctId  "))
  }
 }
} 

当accountFutOpt为Some(帐户)时,结果为Success(Account),符合预期。但是当accountFutOpt为None时,方法的输出为List,如下所示。

account Fut is List()

List()而不是Future应该抛出编译错误吗?

0 个答案:

没有答案