I have an obj: Option[MyObject] variable and you want to extract several variables from it - if the object is not set, use the default values.
I am currently doing it like this:
val var1 = obj match { case Some(o) => e.var1 case _ => "default1" } val var2 = obj match { case Some(o) => e.var2 case _ => "default2" } ...
which is extremely verbose. I know I can do it like this:
val var1 = if (obj.isDefined) obj.get.var1 else "default1" val var2 = if (obj.isDefined) obj.get.var2 else "default2"
which still seems weird. I know that I can use one big match and return a value object or tuple.
But what I would like was like this:
val var1 = obj ? _.var1 : "default1" val var2 = obj ? _.var2 : "default2"
Is it possible somehow?
source share