返回不同 ReaderT的s3f1
和s3f2
函数的示例:
type FailFast[A] = Either[List[String], A]
trait Service1 { def s1f:Option[Int] = Some(10) }
trait Service2 { def s2f:FailFast[Int] = Right(20) }
import cats.instances.option._
def s3f1: ReaderT[Option, Service1, Int] =
for {
r1 <- ReaderT((_: Service1).s1f)
} yield r1 + 1
import cats.syntax.applicative._
import cats.instances.either._
type ReaderService2FF[A] = ReaderT[FailFast, Service2, A]
def s3f2: ReaderService2FF[Int] =
for {
r1 <- ReaderT((_: Service2).s2f)
r2 <- 2.pure[ReaderService2FF]
} yield r1 + r2
我尝试组合这两个函数,它们返回具有不同F[_]
上下文和依赖关系的读者:ReaderT[Option, Service1, Int]
和ReaderT[FailFast, Service2, Int]
我必须以某种方式F[_]
结合上下文,这意味着FailFast
结合Option
. 我认为,将它结合起来是有意义的FailFast[Option]
:
type Env = (Service1, Service2)
type FFOption[A] = FailFast[Option[A]]
type ReaderEnvFF[A] = ReaderT[FFOption, Env, A]
如何组合 s3f1 和 s3f2:
def c: ReaderEnvFF[Int] =
for {
r1 <- //s3f1
r2 <- //s3f2
} yield r1 + r2