Starting Scala 2.13
, Option
has a when builder for this exact purpose:
Option.when(condition)(result)
For instance:
Option.when(true)(3)
// Option[Int] = Some(3)
Option.when(false)(3)
// Option[Int] = None
Also note Option.unless which promotes the opposite condition.