Scala.Either orElse method

Well there has been a very long discussion on this. Currently as you said Either is unbiased (i.e. Left and Right are treated as same). Hence one does not know what to do if orElse is called? Making it Right biased would had solved the problem (As Tony Morris puts it, it was a mistake)

So some hacks are: Using LeftProjection or RightProjection. Right Projection has utility functions map, flatMap, filter etc)

val a:Either[String, Int]
val b:Either[String, Int]

for{
x <- a.right //returns a RightProjection of a
y <- b.right
} yield (a,b)

Or a.right.getOrElse(b.right). One more solution would be as suggested here:

object Implicits {
    implicit def rightBiasEither[A, B](e: Either[A, B]): Either.RightProjection[A, B] = e.right
  }

import Implicits._
a.getOrElse(b).getOrElse(c)

This way you can keep using it as monad.


Update:

Post Scala 2.12, Either is right-biased, which means that Right is assumed to be the default case to operate on. If it is Left, operations like map and flatMap return the Left value unchanged:

 def doubled(i: Int) = i * 2
 Right(42).map(doubled) // Right(84)
 Left(42).map(doubled)  // Left(42)

You can read more on the Either API.


Starting Scala 2.13, Either#orElse has been made available, (after Either being made right-biased in Scala 2.12):

Right(1) orElse Left(2) // Right(1)
Left(1) orElse Left(2)  // Left(2)
Left(1) orElse Left(2) orElse Right(3) // Right(3)

Tags:

Scala

Either