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

Implement Local and Ask Instances #662

Merged
merged 1 commit into from
Apr 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class CatsMtlSpec extends ZioSpecBase {
type Ctx = String
type Error = String

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

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

checkAllAsync(
"Raise[ZIO[Ctx, Error, _]]",
implicit tc => RaiseTests[ZIO[Ctx, Error, _], Error].raise[Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,7 @@ private[interop] sealed trait CatsSpecBaseLowPriority { this: CatsSpecBase =>

implicit def arbitraryZEnvironment[R: Arbitrary: Tag]: Arbitrary[ZEnvironment[R]] =
Arbitrary(Arbitrary.arbitrary[R].map(ZEnvironment(_)))

implicit def eqForZEnvironment[R]: Eq[ZEnvironment[R]] =
Eq.fromUniversalEquals
}
16 changes: 15 additions & 1 deletion zio-interop-cats/shared/src/main/scala/zio/interop/catsmtl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ package zio.interop

import cats.Applicative
import cats.mtl.*
import zio.{ CanFail, ZIO }
import zio.{ CanFail, 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
}

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 Down