我想我错过了一些东西:
scala> Some(1) collect ({ case n if n > 0 => n + 1; case _ => 0})
res0: Option[Int] = Some(2)
scala> None collect ({ case n if n > 0 => n + 1; case _ => 0})
<console>:6: error: value > is not a member of Nothing
None collect ({ case n if n > 0 => n + 1; case _ => 0})
^
<console>:6: error: value + is not a member of Nothing
None collect ({ case n if n > 0 => n + 1; case _ => 0})
为什么会发生此错误?我想我误解了它collect
的工作原理......