Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从ZIO(Scala)管理类型转换_Scala_Zio - Fatal编程技术网

从ZIO(Scala)管理类型转换

从ZIO(Scala)管理类型转换,scala,zio,Scala,Zio,我需要帮助将ZIO[WsConfig,Throwable,A]类型的值转换为ZManaged[A] 有下一个代码(我可以在IDEA中构建它,没有与类型相关的错误,没有人),但我有???恰到好处 def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] = { val zm: ZIO[WsConfig, Throwable, Z

我需要帮助将ZIO[WsConfig,Throwable,A]类型的值转换为ZManaged[A] 有下一个代码(我可以在IDEA中构建它,没有与类型相关的错误,没有人),但我有???恰到好处

    def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] = {

      val zm: ZIO[WsConfig, Throwable, ZManaged[Any, Throwable, UcpZLayer.Service]] =
        for {
          conf <- ZIO.environment[WsConfig]
          cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf))
          acquire = ZIO(new poolCache(cpool))
          release: (UcpZLayer.Service => zio.ZIO[Any,Nothing,Any]) = (pc: UcpZLayer.Service) => pc.closeAll
          zm: ZManaged[Any, Throwable, UcpZLayer.Service] = ZManaged.make(acquire)(release)
        } yield zm

      val managedConnPool: ZManaged[Any, Throwable, UcpZLayer.Service] = ???

      ZLayer.fromManaged(managedConnPool)
    }

我将任何Zlayer(带有confLayer)与++水平组合,并通过>>>传递到poolCache中。

我建议改为这样做:

    def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, UcpZLayer] =
      (for {
        // Use a Managed directly when access then environment
        // `access` will remove the `Has` wrapping.
        conf  <- ZManaged.access[Config[WsConfig]](_.get)

        // Convert the effect into a no-release managed
        cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_

        // Create the managed
        zm    <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
      } yield zm).toLayer // Convert a `Managed` to `ZLayer` directly
def poolCache(隐式标记:taged[UcpZLayer.Service]):ZLayer[ZenvLogConfCache\ux,Throwable,UcpZLayer]=
(用于{
//在访问环境时直接使用托管数据库
//“access”将删除“Has”包装。

confpaupdaniels

非常感谢。当我尝试直接使用您的建议时,会引发类型错误:

    Required: zio.ZLayer[ZenvLogConfCache_, Throwable, zio.Has[Service]]
    Found:    zio.ZLayer[R with Config[WsConfig], Throwable, zio.Has[poolCache]]
我在下一个表格中重写它

      def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, Has[UcpZLayer.Service]] = {
        val zm: ZManaged[Config[WsConfig], Throwable, poolCache] =
          for {
            // Use a Managed directly when access then environment
            conf <- ZManaged.access[Config[WsConfig]](_.get)
            // Convert the effect into a no-release managed
            cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_
            // Create the managed
            zm <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
          } yield zm
        zm.toLayer // Convert a `Managed` to `ZLayer` directly
      }
def poolCache(隐式标记:taged[UcpZLayer.Service]):ZLayer[ZenvLogConfCache\ux,Throwable,Has[UcpZLayer.Service]={
val zm:ZManaged[Config[WsConfig],可丢弃,池缓存]=
为了{
//在访问环境时直接使用托管数据库

conf也许你想要
ZManaged.fromfeffect
?看看@Alekey N Yakushev的好观点,我应该用
访问打开
Has
。我更新了我的答案,这样未来的读者就不会感到困惑了。
      def poolCache(implicit tag: Tagged[UcpZLayer.Service]): ZLayer[ZenvLogConfCache_, Throwable, Has[UcpZLayer.Service]] = {
        val zm: ZManaged[Config[WsConfig], Throwable, poolCache] =
          for {
            // Use a Managed directly when access then environment
            conf <- ZManaged.access[Config[WsConfig]](_.get)
            // Convert the effect into a no-release managed
            cpool <- Ref.make(new OraConnectionPool(conf.dbconf, conf.ucpconf)).toManaged_
            // Create the managed
            zm <- ZManaged.make(ZIO(new poolCache(cpool)))(_.closeAll)
          } yield zm
        zm.toLayer // Convert a `Managed` to `ZLayer` directly
      }