Skip to content

Commit

Permalink
Merge pull request #3361 from travisbrown/fix/3360
Browse files Browse the repository at this point in the history
Restrict attemptNarrow to subtypes of Throwable
  • Loading branch information
djspiewak authored Mar 19, 2020
2 parents b03df99 + a32c7aa commit 824a4b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
/**
* 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]] =
def attemptNarrow[EE <: Throwable, 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) }

/**
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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]] =
def attemptNarrow[EE <: Throwable](implicit F: ApplicativeError[F, E],
tag: ClassTag[EE],
ev: EE <:< E): F[Either[EE, A]] =
F.attemptNarrow[EE, A](fa)

def attemptT(implicit F: ApplicativeError[F, E]): EitherT[F, E, A] =
Expand Down
6 changes: 3 additions & 3 deletions tests/src/test/scala/cats/tests/ApplicativeErrorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ApplicativeErrorSuite extends CatsSuite {
}

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

Expand All @@ -44,7 +44,7 @@ class ApplicativeErrorSuite extends CatsSuite {
}

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

Expand All @@ -61,7 +61,7 @@ class ApplicativeErrorSuite extends CatsSuite {
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[Str.type]]")
assertTypeError("e3.attemptNarrow[List[Num]]")
}

Expand Down

0 comments on commit 824a4b3

Please sign in to comment.