Skip to content

Commit

Permalink
remove ZEnv from Ask and Local
Browse files Browse the repository at this point in the history
  • Loading branch information
coffius committed Sep 18, 2023
1 parent d884406 commit d2bea86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zio.interop

import cats.implicits.*
import cats.mtl.laws.discipline.*
import cats.mtl.{ Handle, Raise }
import zio.*
import zio.interop.catz.*
import zio.interop.catz.mtl.*
Expand All @@ -13,12 +12,12 @@ class CatsMtlSpec extends ZioSpecBase {

checkAllAsync(
"Ask[ZIO[Ctx, Error, _]]",
implicit tc => AskTests[ZIO[Ctx, Error, _], ZEnvironment[Ctx]].ask[Ctx]
implicit tc => AskTests[ZIO[Ctx, Error, _], Ctx].ask[Ctx]
)

checkAllAsync(
"Local[ZIO[Ctx, Error, _]]",
implicit tc => LocalTests[ZIO[Ctx, Error, _], ZEnvironment[Ctx]].local[ZEnvironment[Ctx], Int]
implicit tc => LocalTests[ZIO[Ctx, Error, _], Ctx].local[Ctx, Int]
)

Unsafe.unsafe { implicit unsafe =>
Expand All @@ -44,7 +43,4 @@ class CatsMtlSpec extends ZioSpecBase {
"Handle[ZIO[Ctx, Error, _]]",
implicit tc => HandleTests[ZIO[Ctx, Error, _], Error].handle[Int]
)

def raiseSummoner[R, E] = Raise[ZIO[R, E, _], E]
def handleSummoner[R, E] = Handle[ZIO[R, E, _], E]
}
42 changes: 25 additions & 17 deletions zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,12 @@ package zio.interop

import cats.Applicative
import cats.mtl.*
import zio.{ CanFail, FiberRef, ZEnvironment, ZIO }
import zio.{ CanFail, FiberRef, Tag, ZEnvironment, ZIO }
import zio.internal.stacktracer.InteropTracer

abstract class CatsMtlPlatform extends CatsMtlInstances

abstract class CatsMtlInstances {

implicit def zioLocal[R, E](implicit ev: Applicative[ZIO[R, E, _]]): Local[ZIO[R, E, _], ZEnvironment[R]] =
new Local[ZIO[R, E, _], ZEnvironment[R]] {
override def applicative: Applicative[ZIO[R, E, _]] = ev
override def ask[E2 >: ZEnvironment[R]]: ZIO[R, E, E2] = ZIO.environment
override def local[A](fa: ZIO[R, E, A])(f: ZEnvironment[R] => ZEnvironment[R]): ZIO[R, E, A] =
fa.provideSomeEnvironment(f)(InteropTracer.newTrace(f))
}

implicit def zioAsk[R1, R <: R1, E](implicit ev: Applicative[ZIO[R, E, _]]): Ask[ZIO[R, E, _], ZEnvironment[R1]] =
new Ask[ZIO[R, E, _], ZEnvironment[R1]] {
override def applicative: Applicative[ZIO[R, E, _]] = ev
override def ask[R2 >: ZEnvironment[R1]]: ZIO[R, E, R2] = ZIO.environment
}

abstract class CatsMtlInstances extends CatsMtlInstances1 {
implicit def zioHandle[R, E](implicit ev: Applicative[ZIO[R, E, _]]): Handle[ZIO[R, E, _], E] =
new Handle[ZIO[R, E, _], E] {
override def applicative: Applicative[ZIO[R, E, _]] = ev
Expand All @@ -65,3 +50,26 @@ abstract class CatsMtlInstances {
}

}

trait CatsMtlInstances1 {
implicit def zioLocal[R: Tag, E](implicit ev: Applicative[ZIO[R, E, _]]): Local[ZIO[R, E, _], R] =
new Local[ZIO[R, E, _], R] {
override def applicative: Applicative[ZIO[R, E, _]] = ev

override def ask[R1 >: R]: ZIO[R, E, R1] = ZIO.environment[R].map(_.get)

override def local[A](fa: ZIO[R, E, A])(f: R => R): ZIO[R, E, A] =
fa.provideSomeEnvironment({ (env: ZEnvironment[R]) =>
env.update(f)
})(InteropTracer.newTrace(f))
}

implicit def zioAsk[R1: Tag, R <: R1, E](implicit
ev: Applicative[ZIO[R, E, _]]
): Ask[ZIO[R, E, _], R1] =
new Ask[ZIO[R, E, _], R1] {
override def applicative: Applicative[ZIO[R, E, _]] = ev

override def ask[R2 >: R1]: ZIO[R, E, R2] = ZIO.environment[R1].map(_.get)
}
}

0 comments on commit d2bea86

Please sign in to comment.