0

使用返回 Either[Fail, TupleX] 的表达式,我如何折叠结果而不必在成功块中定义本地 val?

// returns Either[Fail, Tuple2[String, String]]
val result = for{
  model <- bindForm(form).right
  key   <- dao.storeKey(model.email, model.password)
} yield (model.email, key)

result fold (
  Conflict(_),
  tuple2 => { // want to define email/key on this line
    val(email,key) = tuple2
    ...
  }
)
4

2 回答 2

4

像这样

result fold (Conflict(_), { case (email, key) => ... })
于 2012-07-04T11:23:01.620 回答
2

这是一个最小的工作示例:

case class Conflict(s: String)

def foo(result: Either[Conflict, Tuple2[String, String]]) = {
  result.fold(
    c => println("left: " + c.toString),
    { case (email, key) => println("right: %s, %s".format(email, key))}
  )
}

foo(Left(Conflict("Hi")))     // left: Conflict(Hi)
foo(Right(("email", "key")))  // right: email, key
于 2012-07-04T11:23:36.080 回答