None of the other answers answer the question as indicated! To get the exact semantics you specified, use:
implicit class BoolToOption(val self: Boolean) extends AnyVal { def toOption[A](value: => A): Option[A] = if (self) Some(value) else None }
Then you can write
myBool.toOption(someResult)
eg:
scala> true.toOption("hi") res5: Option[String] = Some(hi) scala> false.toOption("hi") res6: Option[String] = None
jazmit Dec 28 '15 at 18:50 2015-12-28 18:50
source share