deltanovember deltanovember - 7 months ago 38
Scala Question

Why do I get a "will always yield true" warning when translating the following from Java to Scala?

Below is a fairly standard piece of Java code which has been translated to Scala

val result: ResultSet = statement.executeQuery(query)

while ( {
if (result.getDouble("FIELD") != null) {


Why do I get

warning: comparing values of types Double and Null using `!=' will
always yield true?


If I understand correctly, it's because a Double in scala can never be Null.

Trying something simple out in the console came up with errors:

scala> val d:Double = null
<console>:13: error: type mismatch;
found   : Null(null)
required: Double

On the other hand, a java.lang.Double can be null:

scala> val d:java.lang.Double = null
d: java.lang.Double = null

So your double is probably a Scala Double as opposed to a Java Double

EDIT I poked around a bit, and it looks like using java.sql.ResultSet's getDouble method will return a scala Double object, which accounts for the warning. As for an alternative to checking for null, I'm not sure that was ever an option. In Java, the method returned a primitive double, which would default to 0.0. I may be wrong, but I don't think Java primitives could ever be null.

Edit2 Removed some stuff that was more speculation than answer.