Is there a predefined function x in Scala that combine 2 Options so that
Some(a) x None => Some(a)
None x Some(b) => Some(b)
None x None => None
Yes, this is the orElse
method. It chooses the first defined value, or None
if neither is defined.
scala> Some(1) orElse None
res0: Option[Int] = Some(1)
scala> None orElse Some(1)
res1: Option[Int] = Some(1)
scala> None orElse None
res2: Option[Nothing] = None
scala> Some(1) orElse Some(2)
res3: Option[Int] = Some(1)