0

Each[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 に答える