diff --git a/core/src/main/scala/cats/syntax/either.scala b/core/src/main/scala/cats/syntax/either.scala index d5b3609e07..d435dd0c9d 100644 --- a/core/src/main/scala/cats/syntax/either.scala +++ b/core/src/main/scala/cats/syntax/either.scala @@ -263,6 +263,10 @@ final class EitherOps[A, B](val eab: Either[A, B]) extends AnyVal { * }}} */ def toEitherT[F[_]: Applicative]: EitherT[F, A, B] = EitherT.fromEither(eab) + + def raiseOrPure[F[_]](implicit ev: ApplicativeError[F, A]): F[B] = + ev.fromEither(eab) + } final class EitherObjectOps(val either: Either.type) extends AnyVal { // scalastyle:off ensure.single.space.after.token diff --git a/tests/src/test/scala/cats/tests/EitherTests.scala b/tests/src/test/scala/cats/tests/EitherTests.scala index 47f1c7db2f..053887511c 100644 --- a/tests/src/test/scala/cats/tests/EitherTests.scala +++ b/tests/src/test/scala/cats/tests/EitherTests.scala @@ -1,7 +1,7 @@ package cats package tests -import cats.data.EitherT +import cats.data.{ EitherT, Validated } import cats.laws.discipline._ import cats.kernel.laws.{GroupLaws, OrderLaws} import scala.util.Try @@ -276,4 +276,11 @@ class EitherTests extends CatsSuite { } } + test("raiseOrPure syntax consistent with fromEither") { + val ev = ApplicativeError[Validated[String, ?], String] + forAll { (fa: Either[String, Int]) => + fa.raiseOrPure[Validated[String, ?]] should === (ev.fromEither(fa)) + } + } + }