1. x match { case None => return; case Some( y ) => y.foo() }
2. return x.map( _.foo )
3.
if (x == null)
return
x.foo()
Man, that is tough ... You just don't allow null to enter FP.
I see, I see... So Scala is really FP with classes to organize your functional code.
Imperative programming is eschewed in Scala. Have to mull this over for a while... ;-).
If x was a String, then we'd say
val X = x.toUpper
There are, as you've noticed, lots of ways to do this kind of job. Pick the one that you find easiest to read and write.
Regards, Oliver
Now, if you have performance problems, you might resort to using null. But that's usually kept hidden -- Scala APIs that return or take nulls are very rare.
--
Ziad
100% agree. type safety has saved me so many times that i lost count.
i don't return null or pass null as a parameter, so i never get null or Some(null) later
Geir
I don't know any Scala library which receives or returns null intentionally, except those wrapping Java libraries that work that way. Maybe there are examples, but they are not particularly common.