Scala - 如何将 T 与 in 中的任何一个结合起来进行理解



假设我有以下设置:

def foo: Either[Error, A] = ???
def bar: EitherT[Future, Error, B] = ???
case class Baz(a: A, b: B)

我如何使用理解来实例化类Baz?我尝试过:

val res = for {
a <- foo
b <- bar
} yield Baz(a, b)

但是,结果的类型为Either[Error, Nothing].我不知道在这种情况下正确的返回类型是什么,但显然我不想Nothing......

EitherEitherT结合起来进行理解的正确方法是什么?

使用EitherT.fromEither函数从Either创建EitherT

import cats.data._
import cats.implicits._
def foo[A]: Either[Error, A] = ???
def bar[B]: EitherT[Future, Error, B] = ???
case class Baz[A, B](a: A, b: B)
def res[A, B] = for {
a <- EitherT.fromEither[Future](foo[A])
b <- bar[B]
} yield Baz(a, b)

相关内容

最新更新