Scala: boolean

I have a Boolean and would like to avoid this template:

if (myBool) Option(someResult) else None 

What I would like to do is

 myBool.toOption(someResult) 

Any suggestions with sample code are welcome.

+50
scala
Oct 30 '13 at 18:03
source share
9 answers

Scalaz has a way to do this using BooleanOps.option . This will allow you to write:

 myBool.option(someResult) 

If you do not want to add a Scalaz dependency, just add the following to your code:

 implicit class RichBoolean(val b: Boolean) extends AnyVal { final def option[A](a: => A): Option[A] = if (b) Some(a) else None } 
+56
Oct 30 '13 at 18:13
source share

Option (). collect () is a good template for this kind of thing.

 Option(myBool).collect { case true => someResult } 

from REPL:

 scala> (Option(true).collect { case true => 3 }, Option(false).collect { case true => 3 }) res3: (Option[Int], Option[Int]) = (Some(3),None) 
+16
Mar 26 '15 at 20:20
source share

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 
+12
Dec 28 '15 at 18:50
source share

If you do not mind that someResult is evaluated regardless of the value of myBool , you can also use

 Some(someResult).filter(myBool) 
+7
Dec 28 '15 at 18:52
source share
 scala> PartialFunction.condOpt(5) { case x if true => x } res9: Option[Int] = Some(5) scala> PartialFunction.condOpt(5) { case x if false => x } res10: Option[Int] = None 

Here, the protection stores the condition, and the value passed to condOpt is the value returned if the protection evaluates to true.

+4
Oct 30 '13 at 18:10
source share

Another choice:

 implicit class RichOptionCompanion(val self: Option.type) extends AnyVal { def when[A](cond: Boolean)(value: => A): Option[A] = if(cond) Some(value) else None } 

Using:

 Option.when(foo != "bar") { ... } 
+4
Oct 30 '13 at 18:29
source share
 class RichBool[T](a: Boolean, res:=> T) { def toOption: Option[T] = if (a) Some(res) else None } implicit def boolToRichBool[T](tp: (Boolean, T)): RichBool[T] = new RichBool(tp._1, tp._2); 

This will give you:

 (true, 5).toOption // Some(5); (false, 3).toOption // None 
+1
Oct 30 '13 at 18:06
source share

Here are a couple of things I would like to consider:

 val bool: Boolean = ??? val result = 1337 Option(bool).withFilter(identity).map(_ => result) 

or

 for { opt <- Option(bool) if opt } yield result 
0
Oct 10 '18 at 16:01
source share

Starting with Scala 2.13 , Option has a constructor A): Option [A] rel = "nofollow noreferrer"> when for this specific purpose:

 Option.when(condition)(result) 

For example:

 Option.when(true)(3) // Option[Int] = Some(3) Option.when(false)(3) // Option[Int] = None 

Also note A): Option [A] rel = "nofollow noreferrer"> Option.unless which contributes to the opposite condition.

0
Jan 17 '19 at 22:16
source share



All Articles