N A
N A

Reputation: 841

Alternative for Either pattern matching

Is there any for the following kind of pattern matching of Either? Something like map on both Right and Left to return an Either of different type. I don't want to specify Left and Right everywhere.

val v:Either[Throwable, String] = Right("Hello")
val result: Either[Int, String] = v match {
  case Left(ex) => Left(ex.getMessage.size)
  case Right(m) => Right(m)
}

Upvotes: 3

Views: 553

Answers (1)

N A
N A

Reputation: 841

In the following, you don't have to use pattern matching

v.left.map(_.getMessage).right.map(_)

Upvotes: 5

Related Questions