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

add attemptNarrow to ApplicativeErrorOps #2863

Merged
merged 4 commits into from
Oct 23, 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
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cats

import cats.data.EitherT

import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

Expand Down Expand Up @@ -73,6 +75,12 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
*/
def attemptT[A](fa: F[A]): EitherT[F, E, A] = EitherT(attempt(fa))

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

/**
* Recover from certain errors by mapping them to an `A` value.
*
Expand Down
5 changes: 5 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 @@ -83,6 +85,9 @@ final class ApplicativeErrorOps[F[_], E, A](private val fa: F[A]) extends AnyVal
def attempt(implicit F: ApplicativeError[F, E]): F[Either[E, A]] =
F.attempt(fa)

def attemptNarrow[EE](implicit F: ApplicativeError[F, E], tag: ClassTag[EE], ev: EE <:< E): F[Either[EE, A]] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a classtag here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EE would be erased and we wouldn't be able to pattern match on it otherwise @tkroman

F.attemptNarrow[EE, A](fa)

def attemptT(implicit F: ApplicativeError[F, E]): EitherT[F, E, A] =
F.attemptT(fa)

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 @@ -23,6 +23,43 @@ class ApplicativeErrorSuite extends CatsSuite {
failed.attempt should ===(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("attemptT syntax creates an EitherT") {
failed.attemptT should ===(EitherT[Option, Unit, Int](Option(Left(()))))
}
Expand Down