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?
What you ask for is something in the line of Safe Navigation Operator (?.) of Groovy, andand gem of Ruby, or accessor variant of the existential operator (?.) of CoffeeScript. For such cases, I generally use
?
method of myRichOption[T]
, which is defined as followsand used as follows