Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for Either pattern matching

Tags:

scala

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)
}
like image 704
N A Avatar asked Jun 18 '26 00:06

N A


1 Answers

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

v.left.map(_.getMessage).right.map(_)
like image 90
N A Avatar answered Jun 21 '26 00:06

N A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!