How do I reverse/flip/invert/swap scala's Option?

Viewed 334

Let's say you've got Option[A] you need to convert it to
Some(b : B) for None
and to
None for Some(a : A).

Is there code that does this already? I can write some, but what's the most straightforward way?

1 Answers

The most straightforward way is :

val b : B = new B()
val newA: Option[B] = a.fold(Some(b))(_ => None)
Related