Last active
December 19, 2022 23:34
-
-
Save erdeszt/8aa10f040efb1994aa32ee966b4e2535 to your computer and use it in GitHub Desktop.
Safe ZIO.serviceWith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait ZIO[-R, +E, +A] { | |
} | |
object ZIO { | |
def serviceWithZIO[S]: SWZIOP[S] = new SWZIOP[S] | |
def serviceWith[S]: SWP[S] = new SWP[S] | |
} | |
trait NotZIO[A] | |
trait NotZIOLP { | |
implicit def ok[A]: NotZIO[A] = new NotZIO[A] {} | |
} | |
object NotZIO extends NotZIOLP { | |
implicit def notok1[R, E, A]: NotZIO[ZIO[R, E, A]] = new NotZIO[ZIO[R, E, A]] {} | |
implicit def notok2[R, E, A]: NotZIO[ZIO[R, E, A]] = new NotZIO[ZIO[R, E, A]] {} | |
} | |
class SWZIOP[S] { | |
def apply[R, E, A](f: S => ZIO[R, E, A]): ZIO[R with S, E, A] = { | |
new ZIO[R with S, E, A] {} | |
} | |
} | |
class SWP[S] { | |
def apply[A](f: S => A)(implicit ev: NotZIO[A]): ZIO[Any, Nothing, A] = { | |
new ZIO[Any, Nothing, A] {} | |
} | |
} | |
trait Foo { | |
def bar: ZIO[Any, Nothing, Unit] | |
def baz: String | |
} | |
ZIO.serviceWithZIO[Foo](_.bar) // ok | |
ZIO.serviceWith[Foo](_.baz) // ok | |
ZIO.serviceWith[Foo](_.bar) // ambiguous implicit | |
println("OK") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment