Scala 2.9 后究竟会删除哪些围绕案例类的内容?
我知道计划对案例类进行一些更改,例如禁止它们之间的继承:
scala> case class Foo()
defined class Foo
scala> case class Bar() extends Foo()
<console>:9: warning: case class `class Bar' has case ancestor `class Foo'. Case-to-case inheritance has potentially dangerous bugs which are unlikely to be fixed. You are strongly encouraged to instead use extractors to pattern match on non-leaf nodes.
case class Bar() extends Foo()
^
defined class Bar
或没有参数列表的案例类(对此不确定):
scala> case class Foo
<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 Foo
^
<console>:7: warning: case classes without a parameter list have been deprecated;
use either case objects or case classes with `()' as parameter list.
case class Foo
^
defined class Foo
目前@deprecated 还有什么?
I know that the are some changes planned regarding case classes, like disallowing inheritance between them:
scala> case class Foo()
defined class Foo
scala> case class Bar() extends Foo()
<console>:9: warning: case class `class Bar' has case ancestor `class Foo'. Case-to-case inheritance has potentially dangerous bugs which are unlikely to be fixed. You are strongly encouraged to instead use extractors to pattern match on non-leaf nodes.
case class Bar() extends Foo()
^
defined class Bar
or case classes without parameter list (not sure about that):
scala> case class Foo
<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 Foo
^
<console>:7: warning: case classes without a parameter list have been deprecated;
use either case objects or case classes with `()' as parameter list.
case class Foo
^
defined class Foo
What else is currently @deprecated?
Scala 中的每个类都必须至少有一个非隐式参数部分。如果您不包含其中之一,编译器将为您添加它。
案例类也不例外。但与模式匹配的交互是混乱和错误的根源,这促使了弃用。
正如 Dean 所建议的,通常最好使用案例对象对此进行建模。
我不知道删除对空参数列表案例类的支持的时间表。案例类继承在 2.9.0 中几乎被删除,但这已推迟到下一个主要版本。
进一步阅读:
为什么第一个参数不能类的列表是隐式的吗?
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.
Case classes are no exception. But the interaction with pattern matching is a source of confusion and bugs, which motivates the deprecation.
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?