Scala、ZIO - 如何将任一种转换为 ZIO?

问题描述

我有一个简单的方法签名,返回类型为 EitherT

def run(someEither: Either[SomeException,Statement],id: String): EitherT[Future,EncodingException,ResultSet]

但是更高级别的方法一个签名:

def someMethod(...) : zio.IO[SomeException,SomeResult]

我像这样在 run调用 someMethod 方法

run(someEither,data.getorElse("empty")).bimap(
      { e: SomeException => IO.fail(e) },_ => IO.succeed(entity)
    )

但是我遇到了关于 Type mismatch 的编译错误 - 需要:IO,找到:EitherT。 如何将 EitherT 转换为 zio.IO

解决方法

我认为你可以这样做:

import zio._
import cats.data.EitherT
import scala.concurrent.Future

type ResultSet = ???

val eitherT: EitherT[Future,Exception,ResultSet] = ???
val result: IO[Throwable,ResultSet] = ZIO.fromFuture(_ => eitherT.value).flatMap(ZIO.fromEither)