Reputation: 841
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
Reputation: 841
In the following, you don't have to use pattern matching
v.left.map(_.getMessage).right.map(_)
Upvotes: 5