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

Restrict attemptNarrow to subtypes of Throwable #3361

Merged
merged 1 commit into from
Mar 19, 2020
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/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