7mind / izumi

Productivity-oriented collection of lightweight fancy stuff for Scala toolchain
https://izumi.7mind.io
BSD 2-Clause "Simplified" License
615 stars 66 forks source link

Support for new ZIO environment #941

Closed fillson-shady closed 4 years ago

fillson-shady commented 4 years ago

Hello! I have difficulties with running Distage hello world example on zio:1.0.0-RC18. First problem is that support for ZLayer is probably missing. This code does not complies:

make[Console].fromResource(console.Console.live)

As a workaround you can convert a ZLayer into a ZManaged:

make[Console].fromResource(console.Console.live.build)

Another (blocker for me) problem is inability to use the TraitConstructor:

def provideCake[R: TraitConstructor, A: Tag](fn: R => A): ProviderMagnet[A] = {
  TraitConstructor[R].provider.map(fn)
}

make[UIO[Unit]].from(provideCake(turboFunctionalHelloWorld.provide))

That gives you the compile time error:

Cannot construct an implementation for zio.Has[zio.console.Console.Service]: it contains a type parameter zio.Has[zio.console.Console.Service] (class Has) in type constructor position
    make[UIO[Unit]].from(provideCake(turboFunctionalHelloWorld.provide))

Of course you can provide this dependency by hand:

make[UIO[Unit]].from((env: console.Console) => turboFunctionalHelloWorld.provide(env))

But in real application there will be much more complex dependency graph, so the code from above is not a good solution.

Is there any possibility to solve these issues?

neko-kai commented 4 years ago

0.10.2 will support Has, (will need a new macro, as TraitConstructor won't handle it) working on it...

neko-kai commented 4 years ago

Fixed in https://github.com/7mind/izumi/pull/960

@fillson-shady You can now use

make[X].fromHas(zioEnvCtor)
make[X].fromHas(zmanagedEnvCtor)
make[X].fromHas(zlayerEnvCtor)

def zioEnvCtor: URIO[Has[Dep1] with Has[Dep2],X]
def zmanagedEnvCtor: URManaged[Has[Dep1] with Has[Dep2],X]
def zlayerEnvCtor: URLayer[Has[Dep1] with Has[Dep2],X]

Uniformly for all ZIO, ZManaged and ZLayer types (or other types that implement BIOLocal), without writing provideCake

You can also use environment and parameter dependencies at the same time in one constructor:

make[X].fromHas(zioArgEnvCtor _)

def zioArgEnvCtor(a: Arg1, b: Arg): RIO[Has[Dep1], X]