Scala Programming Language
  1. Scala Programming Language
  2. SI-6943

No compiler warning for valueClassInstance == value


    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: Scala 2.10.2-RC1
    • Component/s: Misc Compiler
    • Labels:
    • Environment:

      Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_30).


      When comparing a value class instance with the value itself, I expect a compiler warning the same way as is issued if comparing unrelated objects.

      In the example below, the instance of S and 10 are recognized as unrelated (as expected), but the value class instance of T and 10 are not recognized as unrelated (although they are never equal). This is dangerous, since I want to use value classes just for the purpose of having e.g. a type-safe implementation of "Size" that can not be mixed by accident with other Int values. So the compiler should enforce comparing a size like this: "currentFileSize == new Size(10)" (where currentFileSize is an instance of the value type Size). Instead, if I write "currentFileSize == 10", I always get "false" and no warning about it.

      scala> class S(val n: Int)
      defined class S
      scala> val s = new S(10)
      s: S = S@56a96eba
      scala> s == 10
      <console>:10: warning: S and Int are unrelated: they will most likely never compare equal
                    s == 10
      res0: Boolean = false
      scala> class T(val n: Int) extends AnyVal
      defined class T
      scala> val t = new T(10)
      t: T = T@a
      scala> t == 10
      res1: Boolean = false


        Paul Phillips added a comment -


          • Assignee:
            Paul Phillips
            Georg Dietrich
          • Votes:
            0 Vote for this issue
            2 Start watching this issue


            • Created: