How to check for null in a single statement in scala?

theTuxRacer picture theTuxRacer · Apr 21, 2011 · Viewed 120k times · Source

In my scala code:

QueueManager.add(getObject)

where getObject is a method that returns an object of type QueueObject.

def getObject : QueuObject = {
    val response = //some response
    return response
}

Is there a way I can check for the response being null, while adding the QueueObject? I know I can do this:

if (getObject != null)
    QueueManager.add(getObject)

But I do not wish to add a level of indentation. Is there an operator that does that inline?

Thanks.

Answer

Jesper picture Jesper · Apr 22, 2011

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)