Simplifying Option[Boolean] expression in Scala
问题 I have code like that: optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false) And Scalastyle tells me that it can be simplified. How? 回答1: You can try the following: Seq(optionBoolean, otherOptionBoolean).forall(_.contains(true)) In Scala 2.13 (it is very similar in prior versions) the forall method is located at IterableOnce, and its implementation is: def forall(p: A => Boolean): Boolean = { var res = true val it = iterator while (res && it.hasNext) res = p(it.next()) res }