Skip to content

Commit

Permalink
Changed Validated orElse functionality to accumulate failures.
Browse files Browse the repository at this point in the history
Also added orThen combinator to preserve old functionality. Closes typelevel#1447
  • Loading branch information
mgzuber committed Nov 3, 2016
1 parent bcc751f commit b91aa21
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,26 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable {

/**
* Return this if it is Valid, or else fall back to the given default.
* The functionality is similar to that of [[orElse]] except for failure accumulation.
*/
def orElse[EE, AA >: A](default: => Validated[EE, AA]): Validated[EE, AA] =
def orThen[EE, AA >: A](default: => Validated[EE, AA]): Validated[EE, AA] =
this match {
case v @ Valid(_) => v
case Invalid(_) => default
}

/**
* If `this` is valid return `this`, otherwise if `that` is valid return `that`, otherwise combine the failures.
* This is similar to [[orThen]] except that here failures are accumulated.
*/
def orElse[EE >: E, AA >: A](that: => Validated[EE, AA])(implicit EE: Semigroup[EE]): Validated[EE, AA] = this match {
case v @ Valid(_) => v
case Invalid(e) => that match {
case v @ Valid(_) => v
case Invalid(ee) => Invalid(EE.combine(e, ee))
}
}

/**
* Converts the value to an Either[E, A]
*/
Expand Down

0 comments on commit b91aa21

Please sign in to comment.