What is a simple / cleaner way to do this?
val o = Some(4) if(o.isDefined) {o.get == 4} else { false }
I tried
o.getOrElse(null) == 4
but this seems wrong, because in the case of isEmpty you end up checking zero against the other side ... which itself may be zero. I need this to be if opt is defined && & opt.get == anything. I feel that some method on Option should just take a function, and I could do it like this:
o.test( (x) => x == 4 )
and this function will be applied only if o.isDefined.
scala
Trenton
source share