Scala:case类默认值和equals / hashCode:Point≠Point(0,0)

时间:2014-04-19 08:23:31

标签: scala

为什么这些集合都不同?

case class Point(x:Int = 0, y:Int = 0)

Set(Point, Point)               // Set(Point)
Set(Point, Point(0,0))          // Set(Point, Point(0,0))
Set(Point(0,0), Point(x=0,y=0)) // Set(Point(0,0), Point(0,0))

set equality也是false。

我认为即使使用默认值,equals和hashCode也会依赖于值,而不是字符串或其他内容。

1 个答案:

答案 0 :(得分:1)

傻我,scala有类型推断

Set(Point)             : scala.collection.immutable.Set[Point.type]
Set(Point, Point(0,0)) : scala.collection.immutable.Set[Serializable]
Set(Point(0,0))        : scala.collection.immutable.Set[Point]