"comparison of values of types X and Y will always yield false" and sbt

1,627 views
Skip to first unread message

Koert Kuipers

unread,
Mar 15, 2016, 3:42:09 PM3/15/16
to scala-user
scala> val x = 1
scala> val y = "abc"
scala> x == y
<console>:13: warning: comparing values of types Int and String using `==' will always yield false
       x == y
         ^
res0: Boolean = false

this warning is nice on the REPL. but when i compile with sbt it gives me no such warning at all. this can lead so subtle bugs. like i forgot a single ._1 somewhere and spend 30 minutes wondering why the output made no sense. makes me feel like i am back in python!

i would like a warning or even better an error for any such code that i try to compile. does anyone know how to do that?

thanks!

Dennis Haupt

unread,
Mar 15, 2016, 4:05:37 PM3/15/16
to Koert Kuipers, scala-user
i use a type checked == 
 
implicit class AnyOps[X](val x:X) extends AnyVal {
   def ===(other:X) = other == x
}
 
it won't compile if the type is different. you can fine tune it as you need it, but for my cases, it's exactly what i need
 
Gesendet: Dienstag, 15. März 2016 um 20:42 Uhr
Von: "Koert Kuipers" <ko...@tresata.com>
An: scala-user <scala...@googlegroups.com>
Betreff: [scala-user] "comparison of values of types X and Y will always yield false" and sbt
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

som-snytt

unread,
Mar 16, 2016, 1:31:15 AM3/16/16
to scala-user, ko...@tresata.com

Nobody ever says nice things about the built-in comparison warnings. Except Koert, of course.

The warnings are (too) conservative:

scala> 1 == "abc"
<console>:12: warning: comparing values of types Int and String using `==' will always yield false
       1 == "abc"

         ^
res0: Boolean = false

scala> "abc" == 1
res1: Boolean = false

but also annoy people with false positives. There are tickets on the issue tracker.

For ordinary equals, a custom lint rule might work, but if you're enumerating rules, you might as well use a type class.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.

Oliver Ruebenacker

unread,
Mar 16, 2016, 1:59:49 PM3/16/16
to som-snytt, scala-user, ko...@tresata.com

     Hello,

  Note that such comparison warnings only work if at least one type extends AnyVal, because anything else could be null.

  In general, == should be used with caution, since its meaning can vary.

scala> Double.NaN == Double.NaN
res2: Boolean = false

scala> class X(val x: Int)
defined class X

scala> new X(1) == new X(1)
<console>:13: warning: comparing a fresh object using `==' will always yield false
       new X(1) == new X(1)
                ^
res10: Boolean = false

scala> case class XY(x: Int, y: Int)
defined class XY

scala> XY(1, 2) == XY(1, 2)
res11: Boolean = true

scala> class XYZ(x: Int, y: Int, val z: Int) extends XY(x, y)
defined class XYZ

scala> new XYZ(1, 2, 3) == new XYZ(1, 2, 100)
res4: Boolean = true

scala> 0.1 + 0.2 == 0.3
res8: Boolean = false

     Best, Oliver


To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Reply all
Reply to author
Forward
0 new messages