我创建了简单的自己的类型:
type MyEnv = ZEnv with Clock with MyRepository
并有Main
方法:
object Main extends App {
lazy val live: ZLayer[Any, Nothing, Has[MyRepository.Service]] = ZLayer.succeed(LiveMyRepository.repository)
val routes = Router[AppTask](
"/" -> MyRoutes.routes
).orNotFound
val server: ZIO[MyEnv, Throwable, Unit] = ZIO.runtime[MyEnv]
.flatMap {
implicit rts =>
BlazeServerBuilder[AppTask]
.bindHttp(config.port, config.host)
.withHttpApp(routes)
.serve
.compile
.drain
}
def run(args: List[String]): ZIO[ZEnv, Nothing, Int] = {
server.provideCustomLayer(live) foldM(
err => putStrLn(s"Execution failed with: $err") *> IO.succeed(1),
_ => IO.succeed(0))
}
但是我在run
方法中遇到了编译错误:
Error:(33, 30) Cannot prove that zio.ZEnv with zio.Has[my.repositories.MyRepository.Service] <:< my.application.Environment.MyEnv.
server.provideCustomLayer(live) foldM(
我不知道如何解决它。我也有与隐式相关的错误server.provideCustomLayer(live)
:
No parameter found for implicits ev: ZEnv with Has[MyRepository.Service] <:< MyEnv
我试图找到一个解决方案,但是ZIO
文档太没用了,其他地方也没有好的解决方案。