Try to avoid using null
in Scala. It's really there only for interoperability with Java. In Scala, use Option
for things that might be empty. If you're calling a Java API method that might return null
, wrap it in an Option
immediately.
def getObject : Option[QueueObject] = {
// Wrap the Java result in an Option (this will become a Some or a None)
Option(someJavaObject.getResponse)
}
Note: You don't need to put it in a val
or use an explicit
return
statement in Scala; the result will be the value of
the last expression in the block (in fact, since there's only one statement, you don't even need a block).
def getObject : Option[QueueObject] = Option(someJavaObject.getResponse)
Besides what the others have already shown (for example calling foreach
on the Option
, which might be slightly confusing), you could also call map
on it (and ignore the result of the map operation if you don't need it):
getObject map QueueManager.add
This will do nothing if the Option
is a None
, and call QueueManager.add
if it is a Some
.
I find using a regular if
however clearer and simpler than using any of these "tricks" just to avoid an indentation level. You could also just write it on one line:
if (getObject.isDefined) QueueManager.add(getObject.get)
or, if you want to deal with null
instead of using Option
:
if (getObject != null) QueueManager.add(getObject)
edit - Ben is right, be careful to not call getObject
more than once if it has side-effects; better write it like this:
val result = getObject
if (result.isDefined) QueueManager.add(result.get)
or:
val result = getObject
if (result != null) QueueManager.add(result)