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

improve liftTo/rethrowT/raiseOrPure syntax to be more flexible #2984

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
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
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))
}
}