Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add orElseRaise syntax for ApplicativeError #2689

Merged
merged 3 commits into from
Mar 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
}