Which things around case classes will be removed after Scala 2.9 exactly?

家住魔仙堡 提交于 2019-12-03 14:39:30
retronym

Every class in Scala must have at least one non-implicit parameter section. If you don't include one, the compiler will add it for you.

scala> class X
defined class X

scala> new X()
res4: X = X@68003589

scala> class Y
defined class Y

scala> new Y()
res5: Y = Y@467f788b

Case classes are no exception. But the interaction with pattern matching is a source of confusion and bugs, which motivates the deprecation.

scala> case class A
<console>:1: warning: case classes without a parameter list have been deprecated;
use either case objects or case classes with `()' as parameter list.
       case class A
                   ^
defined class A

scala> val a = A()
a: A = A()

scala> (a: Any) match { case A => 1; case _ => 2 }
res0: Int = 2

scala> val companion = A
companion: A.type = A

scala> (companion: Any) match { case A => 1; case _ => 2 }
res0: Int = 1

As Dean suggests, it's usually better to model this with a case object.

I'm not aware of a timeline for removing support for empty-param list case classes. Case class inheritance was almost removed in 2.9.0, but that has been deferred until the next major release.

Further Reading:

Why can't the first parameter list of a class be implicit?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!