考虑我要解决的问题的简化版本。这是我当前的实现。
case class Foo()
def addFoo(json: String): EitherT[Future, Exception, Long] = {
def parse(json: String): EitherT[Future, Exception, Foo] = ???
def validate(foo: Foo): EitherT[Future, Exception, Foo] = ???
def save(foo: Foo): EitherT[Future, Exception, Long] = ???
for {
foo <- parse(json)
_ <- validate(foo)
id <- save(foo)
} yield id
}
这感觉不对,因为尽管validate返回了EitherTor Exception,Foo但它只是执行验证并返回异常或未更改的foo值。所以,我们永远不会对正确的部分感兴趣,也Either永远不会使用它。有没有更好地对应“验证”语义的方法?