Skip to content

Commit

Permalink
.raiseError syntax allows an error subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
guersam committed Sep 18, 2018
1 parent 1911e8b commit 9813b36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class ApplicativeErrorExtensionOps[F[_], E](F: ApplicativeError[F, E]) {
}

final class ApplicativeErrorIdOps[E](val e: E) extends AnyVal {
def raiseError[F[_], A](implicit F: ApplicativeError[F, E]): F[A] =
def raiseError[F[_], A](implicit F: ApplicativeError[F, _ >: E]): F[A] =
F.raiseError(e)
}

Expand Down
29 changes: 29 additions & 0 deletions tests/src/test/scala/cats/tests/ApplicativeErrorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,33 @@ class ApplicativeErrorSuite extends CatsSuite {
(().raiseError[OptionWrapper, Int] orElse OptionWrapper(17.some)).option should === (OptionWrapper(17.some).option)
}
}

{
sealed trait Super
case object Sub extends Super

final case class EitherWrapper[A](either: Either[Super, A])

implicit val eitherWrapperApplicativeError: ApplicativeError[EitherWrapper, Super] = {
val ev = ApplicativeError[Either[Super, ?], Super]
new ApplicativeError[EitherWrapper, Super] {

def raiseError[A](e: Super): EitherWrapper[A] =
EitherWrapper(ev.raiseError(e))

def handleErrorWith[A](fa: EitherWrapper[A])(f: Super => EitherWrapper[A]): EitherWrapper[A] =
EitherWrapper(ev.handleErrorWith(fa.either)(f(_).either))

def pure[A](x: A): EitherWrapper[A] =
EitherWrapper(x.asRight)

def ap[A, B](ff: EitherWrapper[A => B])(fa: EitherWrapper[A]): EitherWrapper[B] =
EitherWrapper(ev.ap(ff.either)(fa.either))
}
}

test("raiseError syntax allows an error subtype") {
assert(Sub.raiseError[EitherWrapper, Unit].either.isLeft)
}
}
}

0 comments on commit 9813b36

Please sign in to comment.