Scala semantics equals / hashCode for case classes with traits

I am new to Scala. The following code is given in 2.7.7

abstract class C
case class CC() extends C

trait T

val c1 = CC()
val c2 = new CC() with T
println(c1.hashCode == c2.hashCode,c1 equals c2)

prints

(false,true)

whereas I expected

(false,false)

What am I missing? Thanks in advance.

+3
source share
2 answers

The radius of the class (especially in Scala 2.8), equalities and hash codes are based on the tuple and / or equality of the product, and this class is currently not taken into account. A recent discussion on this issue on the scala -debate mailing list: http://old.nabble.com/Possible-Collision-Issue-with-Product.hashCode-td27026790.html

For what it's worth, here is what it now looks like in 2.8:

Welcome to Scala version 2.8.0.Beta1-RC6 (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class C
defined class C

scala> case class CC() extends C
defined class CC

scala> trait T
defined trait T

scala> val c1 = CC()
c1: CC = CC()

scala> val c2 = new CC() with T
c2: CC with T = CC()

scala> println(c1.hashCode == c2.hashCode,c1 equals c2)
(true,true)
+8
source

, Scala equals case.

, hashCode Scala 2.7.7. (true, true) Scala 2.8.

+2

Source: https://habr.com/ru/post/1728088/


All Articles