Tuesday, August 20, 2013

Default value on Left

Default value on Left

I make a call to external function and in return have several Either. Say
I have
val a = Right("hey")
val b = Right(2)
val c = Left("oops") .....
for{
x <- a.right
y <- b.right
z <- c.right
} yield(User(x,y,z))
But say, if z is a Left as above. Then I wish to give it a default String.
i.e. by
for{
x <- a.right
y <- b.right
z <- c.right.getOrElse(Right("default String")).right
} yield(User(x,y,z))
How can I reduce this: c.right.getOrElse(Right("default String")).right.
It is dirty. c.right.getOrElse("default") will not work as a map on String
returns IndexedSeq.

No comments:

Post a Comment