Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

null

inline

scala

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.

like image 204
theTuxRacer Avatar asked Apr 21 '11 07:04

theTuxRacer


People also ask

How do you check for nulls in Scala?

Similarly, to check for a null reference you can do this: val resource: Option[Resource] = Option(JavaLib. getResource()) if (resource. isEmpty) { // resource is `None` type.

Is there null in Scala?

Nothing - at the bottom of the Scala type hierarchy. Null is the type of the null literal. It is a subtype of every type except those of value classes. Value classes are subclasses of AnyVal, which includes primitive types such as Int, Boolean, and user-defined value classes.

Is not empty in Scala?

Scala Iterator nonEmpty() method with exampleThe nonEmpty method belongs to the concrete value member of the class Abstract Iterator. It is utilized to check whether the stated collection is not empty. Return Type: It returns true if the stated collection contains at least one element else returns false.

Is it good practice to use null in Scala?

As a word of caution (and balance), the Twitter Effective Scala page recommends not overusing Option , and using the Null Object Pattern where it makes sense. As usual, use your own judgment, but try to eliminate all null values using one of these approaches.


1 Answers

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) 
like image 188
Jesper Avatar answered Oct 09 '22 03:10

Jesper