【发布时间】:2017-07-14 04:16:29
【问题描述】:
当我在Map 中找不到密钥时,我正在尝试执行以下操作
val states = Seq(Ca, Wa, Ny)
val tuples: Seq[Any] = for(state <- states) yield {
val mapValue: Option[CustomCaseClass] = someMap.get(key)
mapValue match {
case Some(value) => {
//do some operations and return a tuple (String, String)
}
case _ => //do nothing, just continue the for loop
}
}
对于case _,我什么都不想做,简单地继续循环。但是使用上面的代码,我无法在 tuples 上执行 toMap 并且我收到以下错误 -
Cannot prove that Any <:< (String, String)
这是因为它可能并不总是返回 Seq[(String,String)]。我希望能够对tuples 进行操作,并且我只想在找不到该案例时跳过该案例。任何指导表示赞赏。
【问题讨论】:
标签: scala