Skip to content

Commit

Permalink
Improve the type signature of orElse
Browse files Browse the repository at this point in the history
The whole point of orElse is to drop the left hand value of a Disjunction, no sense having the type that is dropped influence the resulting type.
  • Loading branch information
jedesah committed Oct 16, 2015
1 parent 87c3fb3 commit 68e4e49
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/scala/cats/data/Xor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ sealed abstract class Xor[+A, +B] extends Product with Serializable {

def getOrElse[BB >: B](default: => BB): BB = fold(_ => default, identity)

def orElse[AA >: A, BB >: B](fallback: => AA Xor BB): AA Xor BB =
fold(_ => fallback, _ => this)
def orElse[C, BB >: B](fallback: => C Xor BB): C Xor BB = this match {
case Xor.Left(_) => fallback
case r @ Xor.Right(_) => r
}

def recover[BB >: B](pf: PartialFunction[A, BB]): A Xor BB = this match {
case Xor.Left(a) if pf.isDefinedAt(a) => Xor.right(pf(a))
Expand Down

0 comments on commit 68e4e49

Please sign in to comment.