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>classS(valn:Int)
defined classS
scala>vals=newS(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>classT(valn:Int) extendsAnyVal
defined classT
scala>valt=newT(10)
t:T=T@a
scala> t ==10
res1:Boolean=false
scala>
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: