Scala - 如何将 EitherT 与 Either 结合起来进行理解
Scala - How to Combine EitherT with Either in For Comprehension
假设我有以下设置:
def foo: Either[Error, A] = ???
def bar: EitherT[Future, Error, B] = ???
case class Baz(a: A, b: B)
如何使用 for comprehension 实例化 class Baz
?我试过:
val res = for {
a <- foo
b <- bar
} yield Baz(a, b)
但是,结果的类型为 Either[Error, Nothing]
。我不知道在这种情况下正确的 return 类型是什么,但显然我不想 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)
假设我有以下设置:
def foo: Either[Error, A] = ???
def bar: EitherT[Future, Error, B] = ???
case class Baz(a: A, b: B)
如何使用 for comprehension 实例化 class Baz
?我试过:
val res = for {
a <- foo
b <- bar
} yield Baz(a, b)
但是,结果的类型为 Either[Error, Nothing]
。我不知道在这种情况下正确的 return 类型是什么,但显然我不想 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)