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

Backport attemptNarrow from ApplicativeError to scala_2.11 #3289

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
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package syntax
import cats.data.Validated.{Invalid, Valid}
import cats.data.{EitherT, Validated}

import scala.reflect.ClassTag

trait ApplicativeErrorSyntax {
implicit final def catsSyntaxApplicativeErrorId[E](e: E): ApplicativeErrorIdOps[E] =
new ApplicativeErrorIdOps(e)
Expand Down Expand Up @@ -86,6 +88,12 @@ final class ApplicativeErrorOps[F[_], E, A](private val fa: F[A]) extends AnyVal
def attemptT(implicit F: ApplicativeError[F, E]): EitherT[F, E, A] =
F.attemptT(fa)

/**
* Similar to [[attempt]], but it only handles errors of type `EE`.
*/
def attemptNarrow[EE <: E: ClassTag](implicit F: ApplicativeError[F, E]): F[Either[EE, A]] =
F.recover(F.map(fa)(Right[EE, A](_): Either[EE, A])) { case e: EE => Left[EE, A](e) }

def recover(pf: PartialFunction[E, A])(implicit F: ApplicativeError[F, E]): F[A] =
F.recover(fa)(pf)

Expand Down
37 changes: 37 additions & 0 deletions tests/src/test/scala/cats/tests/ApplicativeErrorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ class ApplicativeErrorSuite extends CatsSuite {
failed.attemptT should ===(EitherT[Option, Unit, Int](Option(Left(()))))
}

test("attemptNarrow[EE] syntax creates an F[Either[EE, A]]") {
trait Err
case class ErrA() extends Err
case class ErrB() extends Err

implicit val eqForErr: Eq[Err] = Eq.fromUniversalEquals[Err]
implicit val eqForErrA: Eq[ErrA] = Eq.fromUniversalEquals[ErrA]
implicit val eqForErrB: Eq[ErrB] = Eq.fromUniversalEquals[ErrB]

val failed: Either[Err, Int] = ErrA().raiseError[Either[Err, *], Int]

failed.attemptNarrow[ErrA] should ===(ErrA().asLeft[Int].asRight[Err])
failed.attemptNarrow[ErrB] should ===(Either.left[Err, Either[ErrB, Int]](ErrA()))
}

test("attemptNarrow works for parametrized types") {
trait T[A]
case object Str extends T[String]
case class Num(i: Int) extends T[Int]

implicit def eqForT[A]: Eq[T[A]] = Eq.fromUniversalEquals[T[A]]
implicit val eqForStr: Eq[Str.type] = Eq.fromUniversalEquals[Str.type]
implicit val eqForNum: Eq[Num] = Eq.fromUniversalEquals[Num]

val e: Either[T[Int], Unit] = Num(1).asLeft[Unit]
e.attemptNarrow[Num] should ===(e.asRight[T[Int]])
assertTypeError("e.attemptNarrow[Str.type]")

val e2: Either[T[String], Unit] = Str.asLeft[Unit]
e2.attemptNarrow[Str.type] should ===(e2.asRight[T[String]])
assertTypeError("e2.attemptNarrow[Num]")

val e3: Either[List[T[String]], Unit] = List(Str).asLeft[Unit]
e3.attemptNarrow[List[Str.type]] should ===(e3.asRight[List[T[String]]])
assertTypeError("e3.attemptNarrow[List[Num]]")
}

test("recover syntax transforms an error to a success") {
failed.recover { case _ => 7 } should ===(Some(7))
}
Expand Down