Skip to content

Commit

Permalink
Add ApplicativeAsk with subtyping (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Sep 21, 2020
1 parent 05a4920 commit 869e9b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ abstract class CatsMtlInstances {
def local[A](f: R => R)(fa: ZIO[R, E, A]): ZIO[R, E, A] = ZIO.accessM(fa provide f(_))
}

implicit def zioApplicativeAsk[R1, R <: R1, E](
implicit ev: Applicative[ZIO[R, E, ?]]
): ApplicativeAsk[ZIO[R, E, ?], R1] =
new DefaultApplicativeAsk[ZIO[R, E, ?], R1] {
override val applicative: Applicative[ZIO[R, E, *]] = ev
override def ask: ZIO[R, Nothing, R1] = ZIO.environment
}

implicit def zioApplicativeHandle[R, E](implicit ev: Applicative[ZIO[R, E, ?]]): ApplicativeHandle[ZIO[R, E, ?], E] =
new DefaultApplicativeHandle[ZIO[R, E, ?], E] {
val functor: Functor[ZIO[R, E, ?]] = ev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class catzMtlSpec extends catzSpecZIOBase {
implicit tc => ApplicativeHandleTests[ZIO[Ctx, Error, *], Error].applicativeHandle[Int]
)

def askSummoner[R, E] = ApplicativeAsk[ZIO[R, E, *], R]
def localSummoner[R, E] = ApplicativeLocal[ZIO[R, E, *], R]
def raiseSummoner[R, E] = FunctorRaise[ZIO[R, E, *], E]
def handleSummoner[R, E] = ApplicativeHandle[ZIO[R, E, *], E]
def askSummoner[R, E] = ApplicativeAsk[ZIO[R, E, *], R]
def askSubtypingSummoner[R1, R <: R1, E] = ApplicativeAsk[ZIO[R, E, *], R1]
def localSummoner[R, E] = ApplicativeLocal[ZIO[R, E, *], R]
def raiseSummoner[R, E] = FunctorRaise[ZIO[R, E, *], E]
def handleSummoner[R, E] = ApplicativeHandle[ZIO[R, E, *], E]
}

0 comments on commit 869e9b9

Please sign in to comment.