0

With an expression that returns an Either[Fail, TupleX], how do I fold over the result without having to define local vals in the success block?

// 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
    ...
  }
)
virtualeyes
  • 11,147
  • 6
  • 56
  • 91

2 Answers2

4

Like this

result fold (Conflict(_), { case (email, key) => ... })
Yo Eight
  • 467
  • 2
  • 5
2

Here is a minimal working example:

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
Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71