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

case value class canEqual is inconsistent #13034

Open
lrytz opened this issue Sep 12, 2024 · 1 comment
Open

case value class canEqual is inconsistent #13034

lrytz opened this issue Sep 12, 2024 · 1 comment
Milestone

Comments

@lrytz
Copy link
Member

lrytz commented Sep 12, 2024

scala> case class VCC(x: Int) extends AnyVal
class VCC

scala> val v = VCC(1)
val v: VCC = VCC(1)

scala> v.canEqual(v)
val res0: Boolean = false

scala> v.canEqual(1)
val res1: Boolean = true

scala> v == v
val res2: Boolean = true

Fortunately, equals doesn't invoke canEqual because the class is final (it's a value class) https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala#L200.

@lrytz
Copy link
Member Author

lrytz commented Sep 12, 2024

... somewhat related: value case classes don't take canEquals into account when they should. It works for final case classes.

scala> case class VCC(x: Int) extends AnyVal { def canEqual(x: Any) = false }
class VCC

scala> VCC(1) == VCC(1)
val res0: Boolean = true

scala> final case class FCC(x: Int) { def canEqual(x: Any) = false }
class FCC

scala> FCC(1) == FCC(1)
val res2: Boolean = false

@mburlo mburlo removed their assignment Sep 12, 2024
@lrytz lrytz assigned lrytz and mburlo and unassigned lrytz and mburlo Sep 12, 2024
@SethTisue SethTisue added this to the 2.13.16 milestone Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants