How to check for null or false in Scala concisely?

2019-02-01 05:30发布

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?

标签: scala null
7条回答
走好不送
2楼-- · 2019-02-01 06:14

Using pattern matching as suggested in a couple of answers here is a nice approach:

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

But, a bit verbose.

I prefer to map an Option in the following way:

Option(getSomething()) map (something -> doSomethingWith(something))

One liner, short, clear.

The reason to that is Option can be viewed as some kind of collection – some special snowflake of a collection that contains either zero elements or exactly one element of a type and as as you can map a List[A] to a List[B], you can map an Option[A] to an Option[B]. This means that if your instance of Option[A] is defined, i.e. it is Some[A], the result is Some[B], otherwise it is None. It's really powerful!

查看更多
冷血范
3楼-- · 2019-02-01 06:17

You could write some wrapper yourself or use an Option type.

I really wouldn't check for null though. If there is a null somewhere, you should fix it and not build checks around it.

Building on top of axel22's answer:

implicit def any2hm(x: Any) = new {
  def ? = x match {
    case null => false
    case false => false
    case 0 => false
    case s: String if s.isEmpty => false
    case _ => true
  }
}

Edit: This seems to either crash the compiler or doesn't work. I'll investigate.

查看更多
成全新的幸福
4楼-- · 2019-02-01 06:18

Well, Boolean cannot be null, unless passed as a type parameter. The way to handle null is to convert it into an Option, and then use all the Option stuff. For example:

Option(some) foreach { s => println(s) }
Option(some) getOrElse defaultValue

Since Scala is statically type, a thing can't be "a null or is empty string or is zero number etc". You might pass an Any which can be any of those things, but then you'd have to match on each type to be able to do anything useful with it anyway. If you find yourself in this situation, you most likely are not doing idiomatic Scala.

查看更多
Deceive 欺骗
5楼-- · 2019-02-01 06:18

If you use extempore's null-safe coalescing operator, then you could write your str example as

val str = ?:(some)(_.toString)()

It also allows you to chain without worrying about nulls (thus "coalescing"):

val c = ?:(some)(_.toString)(_.length)()

Of course, this answer only addresses the second part of your question.

查看更多
女痞
6楼-- · 2019-02-01 06:20

In Scala, the expressions you described mean that a method called ? is invoked on an object called some. Regularly, objects don't have a method called ?. You can create your own implicit conversion to an object with a ? method which checks for nullness.

implicit def conversion(x: AnyRef) = new {
  def ? = x ne null
}

The above will, in essence, convert any object on which you call the method ? into the expression on the right hand side of the method conversion (which does have the ? method). For example, if you do this:

"".?

the compiler will detect that a String object has no ? method, and rewrite it into:

conversion("").?

Illustrated in an interpreter (note that you can omit . when calling methods on objects):

scala> implicit def any2hm(x: AnyRef) = new {
     |   def ? = x ne null
     | }
any2hm: (x: AnyRef)java.lang.Object{def ?: Boolean}

scala> val x: String = "!!"
x: String = "!!"

scala> x ?
res0: Boolean = true

scala> val y: String = null
y: String = null

scala> y ?
res1: Boolean = false

So you could write:

if (some ?) {
  // ...
}

Or you could create an implicit conversion into an object with a ? method which invokes the specified method on the object if the argument is not null - do this:

scala> implicit def any2hm[T <: AnyRef](x: T) = new {
     |   def ?(f: T => Unit) = if (x ne null) f(x)
     | }
any2hm: [T <: AnyRef](x: T)java.lang.Object{def ?(f: (T) => Unit): Unit}

scala> x ? { println }
!!

scala> y ? { println }

so that you could then write:

some ? { _.toString }

Building (recursively) on soc's answer, you can pattern match on x in the examples above to refine what ? does depending on the type of x. :D

查看更多
Anthone
7楼-- · 2019-02-01 06:30

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.

查看更多
登录 后发表回答