61

根据scala-wartremover静态分析工具,我必须在我创建的每个案例类前面加上“最终”:错误消息说“案例类必须是最终的”。

根据替罪羊(Scala的另一个静态分析工具),我不应该(错误消息:“案例类上的冗余最终修饰符”)

谁是对的,为什么?

4

1 回答 1

108

从某种意义上说,使用它确实会改变事情,这并不是多余的。正如人们所期望的那样,您不能扩展最终案例类,但可以扩展非最终案例类。为什么 wartremover 建议案例类应该是最终的?好吧,因为扩展它们并不是一个好主意。考虑一下:

scala> case class Foo(v:Int)
defined class Foo

scala> class Bar(v: Int, val x: Int) extends Foo(v)
defined class Bar

scala> new Bar(1, 1) == new Bar(1, 1)
res25: Boolean = true

scala> new Bar(1, 1) == new Bar(1, 2)
res26: Boolean = true
// ????

真的吗?Bar(1,1)等于Bar(1,2)?这是出乎意料的。但是等等,还有更多:

scala> new Bar(1,1) == Foo(1)
res27: Boolean = true

scala> class Baz(v: Int) extends Foo(v)
defined class Baz

scala> new Baz(1) == new Bar(1,1)
res29: Boolean = true //???

scala> println (new Bar(1,1))
Foo(1) // ???

scala> new Bar(1,2).copy()
res49: Foo = Foo(1) // ???

Barhas type的副本Foo?这可能是对的吗?

当然,我们可以通过在 and 上覆盖.equals(and .hashCode, and .toString, and .unapply, and .copy, and also, possible, .productIterator, .productArity, .productElementetc.) 方法Bar来解决这个问题Baz。但是“开箱即用”,任何扩展案例类的类都会被破坏。

这就是原因,你不能再用另一个案例类扩展一个案例类,我认为 scala 2.11 从那以后就被禁止了。仍然允许通过非案例类扩展案例类,但至少在 wartremover 看来,这并不是一个好主意。

于 2016-01-02T02:32:51.187 回答