You can use the PartialFunction
companion object and condOpt
:
PartialFunction.condOpt(condition) {case true => result}
Usage:
scala> PartialFunction.condOpt(false) {case true => 42}
res0: Option[Int] = None
scala> PartialFunction.condOpt(true) {case true => 42}
res1: Option[Int] = Some(42)