简体   繁体   English

如何包装有效的 F 而不是具体的类型构造函数?

[英]How to wrap effectful F instead of the concrete type constructor?

I have the following function definition:我有以下 function 定义:

  private val checkSapHealth: IO[ServerHealth] =
    BlazeClientBuilder[IO](global).resource.use { client =>
      discovery
        .senderAddr
        .flatMap { addr =>
          client
            .get(addr.url |+| addr.health) { res =>
              res.status match {
                case Ok =>
                  IO(ServerOnline)
                case _ =>
                  IO(ServerOffline)
              }
            }
            .timeout(2.second)
            .recover {
              case _: Throwable => ServerOffline
            }

        }
    }

I would like to replace the concrete type IO through F[_] to make it more abstract.我想通过F[_]替换具体类型IO使其更抽象。 The problem here is the line:这里的问题是:

IO(ServerOnline)

The question is how to make it to问题是如何做到

F(ServerOnline)

Try to use cats.effect.Sync尝试使用cats.effect.Sync

https://typelevel.org/cats-effect/typeclasses/sync.html https://typelevel.org/cats-effect/typeclasses/sync.html

So basically using Sync[IO].delay is equivalent to using IO.apply .所以基本上使用Sync[IO].delay相当于使用IO.apply

private def checkSapHealth[F[_]: Sync]: F[ServerHealth] = ...

use ConcurrentEffect and add an implicit Applicative of F, thus giving you the option to lift values into the F context使用ConcurrentEffect并添加 F 的隐式 Applicative,从而使您可以选择将值提升到 F 上下文中

private def checkSapHealth[F[_] : ConcurrentEffect](implicit A: Applicative[F]): F[ServerHealth] =
  BlazeClientBuilder[F](global).resource.use { client =>
    discovery
      .senderAddr
      .flatMap { addr =>
        client
          .get(addr.url |+| addr.health) { res =>
            res.status match {
              case Ok =>
                A.pure(ServerOnline)
              case _ =>
                A.pure(ServerOffline)
            }
          }
          .timeout(2.second)
          .recover {
            case _: Throwable => ServerOffline
          }
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查WeakTypeTag或Type是否代表具体类型? - How to check if WeakTypeTag or Type represents concrete type? 在scala反射中,如何解析具体类型成员? - In scala reflection, how to resolve concrete type member? scala:如何包装子类构造函数的执行? - scala: how to wrap execution of subclass constructor? 通过类型成员而不是类型参数进行F-限制量化? - F-bounded quantification through type member instead of type parameter? 在需要具体类型的情况下,如何使用存在类型的变量? - How to use a variable with existential type where a concrete type is required? Scala中某个抽象类型的类型参数对应的具体类型如何体现? - How to reflect concrete types that corresponds to the type parameters of an abstraction type in Scala? 在Scala Reflection中,如何获取具体子类的泛型类型参数? - In Scala Reflection, How to get generic type parameter of a concrete subclass? 将T *表示为具体类型 - Represent T* as concrete type 如何指示抽象成员将在Scala中返回具体类型 - How to indicate an abstract member will return the concrete type in Scala 为什么方法参数 F 可以与类型构造函数 F 同名? - Why can method parameter F have the same name as type constructor F?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM