Skip to content

Commit

Permalink
improve liftTo/rethrowT/raiseOrPure syntax to be more flexible (#2984)
Browse files Browse the repository at this point in the history
* improve liftTo/rethrowT syntax to be more flexible

* deprecate Either.raiseOrPure; use Either.liftTo instead
  • Loading branch information
bpholt authored and kailuowang committed Aug 16, 2019
1 parent 0de7503 commit 481b5ff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
/**
* Inverse of `MonadError#attemptT`
*/
def rethrowT(implicit F: MonadError[F, A]): F[B] =
def rethrowT(implicit F: MonadError[F, _ >: A]): F[B] =
F.rethrow(value)

def valueOr[BB >: B](f: A => BB)(implicit F: Functor[F]): F[BB] = fold(f, identity)
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ final class EitherOps[A, B](private val eab: Either[A, B]) extends AnyVal {

def toEitherNel[AA >: A]: EitherNel[AA, B] = leftMap(NonEmptyList.one)

@deprecated("use liftTo instead", "2.0.0")
def raiseOrPure[F[_]](implicit ev: ApplicativeError[F, A]): F[B] =
ev.fromEither(eab)

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait ValidatedExtensionSyntax {
}

final class ValidatedExtension[E, A](private val self: Validated[E, A]) extends AnyVal {
def liftTo[F[_]](implicit F: ApplicativeError[F, E]): F[A] =
def liftTo[F[_]](implicit F: ApplicativeError[F, _ >: E]): F[A] =
new ApplicativeErrorExtensionOps(F).fromValidated(self)
}

Expand Down
9 changes: 9 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ class EitherTSuite extends CatsSuite {
failed.attemptT.rethrowT should ===(failed)
}

test("rethrowT works with specialized failures") {
implicit val eqThrow: Eq[Throwable] = Eq.fromUniversalEquals
val failed: Try[Int] = Failure(new IllegalArgumentException("error"))

val t: EitherT[Try, IllegalArgumentException, Int] =
failed.attemptT.leftMap(_.asInstanceOf[IllegalArgumentException])
t.rethrowT should ===(failed)
}

test("transform consistent with value.map") {
forAll { (eithert: EitherT[List, String, Int], f: Either[String, Int] => Either[Long, Double]) =>
eithert.transform(f) should ===(EitherT(eithert.value.map(f)))
Expand Down
9 changes: 9 additions & 0 deletions tests/src/test/scala/cats/tests/ValidatedSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,13 @@ class ValidatedSuite extends CatsSuite {
v.liftTo[Option] shouldBe v.toOption
}
}

test("liftTo works with specialized errors") {
implicit val eqThrow: Eq[Throwable] = Eq.fromUniversalEquals
val ex: IllegalArgumentException = new IllegalArgumentException()
val validated: Validated[IllegalArgumentException, Int] = Validated.Invalid(ex)
val lifted: Either[Throwable, Int] = validated.liftTo[Either[Throwable, *]]

lifted should ===(Left[Throwable, Int](ex))
}
}

0 comments on commit 481b5ff

Please sign in to comment.