Skip to content

Instantly share code, notes, and snippets.

@erdeszt
Last active December 19, 2022 23:34
Show Gist options
  • Save erdeszt/8aa10f040efb1994aa32ee966b4e2535 to your computer and use it in GitHub Desktop.
Save erdeszt/8aa10f040efb1994aa32ee966b4e2535 to your computer and use it in GitHub Desktop.
Safe ZIO.serviceWith
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