Thursday, December 13, 2012
Here's something that scala folks should probably use more often Option.isDefined
Sometimes its much simpler to get something that returns an Option, and then simply test isDefined. Rather that doing a full blown match.
For example
if( foo().isDefined() ){
doSomething()
}
else {
doSomethingElse()
}
rather than
foo() match {
case Some( o : Object ) => {
doSomething()
}
case _ => {
doSomethingElse()
}
}
I think you can argue over readability.
But I think the first example emphasizes the intent that the programmer is reacting to the point that foo is returning something vs. returning nothing (or None).
Also, one point in the first one. Sometimes the programmer doesn't really care what was returned by foo.
Subscribe to:
Posts (Atom)