Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for null or false in Scala concisely?

Tags:

null

scala

In Groovy language, it is very simple to check for null or false like:

groovy code:

def some = getSomething() if(some) { // do something with some as it is not null or emtpy   } 

In Groovy if some is null or is empty string or is zero number etc. will evaluate to false. What is similar concise method of testing for null or false in Scala? What is the simple answer to this part of the question assuming some is simply of Java type String?

Also another even better method in groovy is:

def str = some?.toString() 

which means if some is not null then the toString method on some would be invoked instead of throwing NPE in case some was null. What is similar in Scala?

like image 200
ace Avatar asked Jun 20 '11 20:06

ace


People also ask

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.

How do you handle null values in Scala?

In Scala, using null to represent nullable or missing values is an anti-pattern: use the type Option instead. The type Option ensures that you deal with both the presence and the absence of an element. Thanks to the Option type, you can make your system safer by avoiding nasty NullPointerException s at runtime.

What is difference between null and null in Scala?

2. Null and null. The null reference is used to represent an absent value, and Null with a capital 'N' is its type.

Is empty string null in Scala?

If you're looking to check whether a String is null or empty, here's a one-liner that does that. You wrap it with an Option, use filter to make sure it's not an empty String then call getOrElse to provide a default value in case the original value is either null or an empty String.


1 Answers

What you may be missing is that a function like getSomething in Scala probably wouldn't return null, empty string or zero number. A function that might return a meaningful value or might not would have as its return an Option - it would return Some(meaningfulvalue) or None.

You can then check for this and handle the meaningful value with something like

 val some = getSomething()  some match {     case Some(theValue) => doSomethingWith(theValue)     case None           => println("Whoops, didn't get anything useful back")  } 

So instead of trying to encode the "failure" value in the return value, Scala has specific support for the common "return something meaningful or indicate failure" case.

Having said that, Scala's interoperable with Java, and Java returns nulls from functions all the time. If getSomething is a Java function that returns null, there's a factory object that will make Some or None out of the returned value.

So

  val some = Option(getSomething())   some match {     case Some(theValue) => doSomethingWith(theValue)     case None           => println("Whoops, didn't get anything useful back")   } 

... which is pretty simple, I claim, and won't go NPE on you.

The other answers are doing interesting and idiomatic things, but that may be more than you need right now.

like image 167
The Archetypal Paul Avatar answered Sep 22 '22 20:09

The Archetypal Paul