假设我有以下设置:
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
......
将Either
和EitherT
结合起来进行理解的正确方法是什么?
使用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)