[scala] How to check for null in a single statement in 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.

This question is related to scala null inline

The answer is


Option(getObject) foreach (QueueManager add)

Although I'm sure @Ben Jackson's asnwer with Option(getObject).foreach is the preferred way of doing it, I like to use an AnyRef pimp that allows me to write:

getObject ifNotNull ( QueueManager.add(_) )

I find it reads better.

And, in a more general way, I sometimes write

val returnVal = getObject ifNotNull { obj =>
  returnSomethingFrom(obj)
} otherwise {
  returnSomethingElse
}

... replacing ifNotNull with ifSome if I'm dealing with an Option. I find it clearer than first wrapping in an option and then pattern-matching it.

(For the implementation, see Implementing ifTrue, ifFalse, ifSome, ifNone, etc. in Scala to avoid if(...) and simple pattern matching and the Otherwise0/Otherwise1 classes.)


If it instead returned Option[QueueObject] you could use a construct like getObject.foreach { QueueManager.add }. You can wrap it right inline with Option(getObject).foreach ... because Option[QueueObject](null) is None.


Examples related to scala

Intermediate language used in scalac? Why does calling sumr on a stream with 50 tuples not complete Select Specific Columns from Spark DataFrame Joining Spark dataframes on the key Provide schema while reading csv file as a dataframe how to filter out a null value from spark dataframe Fetching distinct values on a column using Spark DataFrame Can't push to the heroku Spark - Error "A master URL must be set in your configuration" when submitting an app Add jars to a Spark Job - spark-submit

Examples related to null

getElementById in React Filter values only if not null using lambda in Java8 Why use Optional.of over Optional.ofNullable? How to resolve TypeError: Cannot convert undefined or null to object Check if returned value is not null and if so assign it, in one line, with one method call How do I assign a null value to a variable in PowerShell? Using COALESCE to handle NULL values in PostgreSQL How to check a Long for null in java Check if AJAX response data is empty/blank/null/undefined/0 Best way to check for "empty or null value"

Examples related to inline

Java - Check Not Null/Empty else assign default value How can I display two div in one line via css inline property Send inline image in email How to pass event as argument to an inline event handler in JavaScript? Adding a stylesheet to asp.net (using Visual Studio 2010) inline if statement java, why is not working how to create inline style with :before and :after How to write inline if statement for print? Logo image and H1 heading on the same line CSS two div width 50% in one line with line break in file