Skip to content

Commit

Permalink
Add orElseRaise syntax for ApplicativeError (#2689)
Browse files Browse the repository at this point in the history
* Add `orElseRaise`

* Rename orElseRaise to orRaise, add doctest

* Use adaptErr for implementation instead of orElse
  • Loading branch information
kubukoz authored Mar 29, 2020
1 parent 2a73383 commit 97dbfeb
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,25 @@ final class ApplicativeErrorOps[F[_], E, A](private val fa: F[A]) extends AnyVal
* this would result in ambiguous implicits.
*/
def adaptErr(pf: PartialFunction[E, E])(implicit F: ApplicativeError[F, E]): F[A] = F.adaptError(fa)(pf)

/**
* Handle all errors on this F[A] by raising an error using the given value.
*
* Example:
* {{{
* scala> import cats._, implicits._
*
* scala> val fa: Either[String, Int] = Left("wrong")
*
* scala> fa.orRaise("wronger")
* res1: Either[String,Int] = Left(wronger)
*
* scala> val fb: Either[String, Int] = Right(42)
*
* scala> fb.orRaise("wrongest")
* res2: Either[String,Int] = Right(42)
* }}}
*/
def orRaise(other: => E)(implicit F: ApplicativeError[F, E]): F[A] =
adaptErr { case _ => other }
}

0 comments on commit 97dbfeb

Please sign in to comment.