Is there a good way to access a parameter value inside an Option object? Nested matches lead to an ugly tree structure.
So, if I have, for example:
case class MyObject(value: Option[Int])
val optionObject : Option[MyObject] = Some(MyObject(Some(2))
The only thing I know to access the value will be:
val defaultCase = 0
optionObject match {
case Some(o) => o.value match {
case Some(number) => number
case None => defaultCase
}
case None => defaultCase
}
And this is pretty ugly, because this design is only for access to one small parameter value.
What I would like to do is something like:
optionObject.value.getOrElse(0)
or how you can do with Swift :
if (val someVal = optionObject.value) {
}
Is there anything in Scala that allows me to handle these things well?
Thank!
source
share