我是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]
。我知道这样做的原因是因为理解实际上是一些句法糖覆盖了一些flatMaps
和maps
,但我不知道如何返回Option
的内容。我不想使用Option.get
,因为我已经阅读了这很糟糕的做法(因为它本质上使Option
的全部点无效。所以,我还能做到这一点?
预先感谢。
您可以执行.getOrElse(List.empty)