Skip to content

Commit

Permalink
Add raiseOption method to as an extension method of Option. (#3358)
Browse files Browse the repository at this point in the history
Within an effect F with ApplicativeError instance, this allows us to
raise an option if there is Some error, or skip if it there is None.
  • Loading branch information
diesalbla authored Jun 21, 2020
1 parent e256ab3 commit 1411dcf
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/syntax/option.scala
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,27 @@ final class OptionOps[A](private val oa: Option[A]) extends AnyVal {
*/
def liftTo[F[_]]: LiftToPartiallyApplied[F, A] = new LiftToPartiallyApplied(oa)

/**
* Raise to an F[Unit], as long as it has an ApplicativeError[F, A] instance
* If the option is empty, an empty unit effect is given.
* If the option contains an error, it is raised.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> type F[A] = Either[String, A]
*
* scala> Option.empty[String].raiseTo[F]
* res0: scala.Either[String, Unit] = Right(())
*
* scala> Option("Failed").raiseTo[F]
* res1: scala.Either[String, Unit] = Left(Failed)
* }}}
*/
def raiseTo[F[_]](implicit F: ApplicativeError[F, A]): F[Unit] =
oa.fold(F.unit)(F.raiseError)

/**
* Transform the `Option` into a [[cats.data.OptionT]] while lifting it into the specified Applicative.
*
Expand Down

0 comments on commit 1411dcf

Please sign in to comment.