理解返回选项[list [t]]];想要列表[value]



我是Scala的新手,并且很难确切地弄清楚如何提取 Option

我的代码可以:

getResult( name, age, id).map(response =>
  for {
    accounts <- response._id_list // response.account_id_list is an Option[String]
    ageList <- response.age_list // response.details is an Option[Details]
  } yield {
      accounts.split(" ").map(accountID =>  Account(
        accountID = accountID,
      ))
  }
)

这返回Option[List[Account]],但我只想返回List[Account]。我知道这样做的原因是因为理解实际上是一些句法糖覆盖了一些flatMapsmaps,但我不知道如何返回Option的内容。我不想使用Option.get,因为我已经阅读了这很糟糕的做法(因为它本质上使Option的全部点无效。所以,我还能做到这一点?

预先感谢。

您可以执行.getOrElse(List.empty)

最新更新