如何 return 离开已验证的项目?

How to return Left item from Validated?

我有一个方法 returns 以下内容:

def myMethod(): Validated[List[MyError], MyClass] {
 ...
}

我有另一种方法需要 List[MyError]

def otherMethod(errors: List[MyError]) { 
  ...
}

如何调用 otherMethod 并从 myMethod 返回 List[MyError]。如下所示:

otherMethod(myMethod())

以上不工作并给出编译错误:

expected: List[MyError], actual: Validated[List[MyError], MyClass]

myMethod执行有两种可能的结果:或者。 returns 错误列表或(大概)MyClass 类型的结果。 您只是在谈论处理这些结果之一。那是错的。你应该始终牢记两者,并一起处理它们。例如:

   myMethod() match {
     case Left(errors) => otherMethod(errors)
     case Right(result) => yetAnotherMethod(result)
   }