From d2bea86e201f58cb17dc59f9ef96d8bed2fb0c0c Mon Sep 17 00:00:00 2001 From: Aleksei Shamenev Date: Mon, 18 Sep 2023 20:33:54 +0300 Subject: [PATCH] remove ZEnv from Ask and Local --- .../test/scala/zio/interop/CatsMtlSpec.scala | 8 +--- .../src/main/scala/zio/interop/catsmtl.scala | 42 +++++++++++-------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/zio-interop-cats-tests/shared/src/test/scala/zio/interop/CatsMtlSpec.scala b/zio-interop-cats-tests/shared/src/test/scala/zio/interop/CatsMtlSpec.scala index 696bc4df..f97c99d4 100644 --- a/zio-interop-cats-tests/shared/src/test/scala/zio/interop/CatsMtlSpec.scala +++ b/zio-interop-cats-tests/shared/src/test/scala/zio/interop/CatsMtlSpec.scala @@ -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.* @@ -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 => @@ -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] } diff --git a/zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala b/zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala index 9df3f419..d261ccd8 100644 --- a/zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala +++ b/zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala @@ -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 @@ -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) + } +}