Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No compiler warning for valueClassInstance == value #6943

Closed
scabug opened this issue Jan 8, 2013 · 2 comments
Closed

No compiler warning for valueClassInstance == value #6943

scabug opened this issue Jan 8, 2013 · 2 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Jan 8, 2013

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

scala>
@scabug
Copy link
Author

@scabug scabug commented Jan 8, 2013

Imported From: https://issues.scala-lang.org/browse/SI-6943?orig=1
Reporter: Georg Dietrich (ugdietrich)
Affected Versions: 2.10.0

@scabug
Copy link
Author

@scabug scabug commented Apr 24, 2013

@scabug scabug closed this Apr 25, 2013
@scabug scabug added this to the 2.10.2-RC1 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants