哪些事情在case类将被删除后Scala 2.9? [英] Which things around case classes will be removed after Scala 2.9 exactly?
问题描述
scala>我们知道, case类Foo()
定义类Foo
scala> case class Bar()extends Foo()
< console>:9:warning:case class`class Bar'有case ancestor`class Foo'。案例间继承具有潜在的危险错误,这些错误不太可能被修复。强烈建议您使用提取器在非叶节点上进行模式匹配。
case class Bar()extends Foo()
^
定义类Bar
$ b b
或不带参数列表的case类(不确定):
scala> case类Foo
< console>:1:warning:没有参数列表的case类已被弃用;
使用case对象或case类与`()'作为参数列表。
case class Foo
^
< console>:7:warning:没有参数列表的case类已被弃用;
使用case对象或case类与`()'作为参数列表。
case class Foo
^
定义类Foo
$ p
Scala中的每个类必须至少有一个非隐式参数部分。如果你不包括一个,编译器会为你添加它。
scala> class X
defined class X
scala> new X()
res4:X = X @ 68003589
scala>类Y
定义类Y
scala>案例类别也不例外。新的Y()
res5:Y = Y @ 467f788b
。但是与模式匹配的交互是一个混乱和错误的来源,这激发了贬低。
scala> case类A
< console>:1:warning:不带参数列表的case类已被弃用;
使用case对象或case类与`()'作为参数列表。
case class A
^
定义类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
正如Dean所说,与一个case对象。
我不知道一个时间线删除对空参数列表case类的支持。案例类继承在2.9.0中几乎被删除,但是已经推迟到下一个主要版本。
进一步阅读:
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?
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?
这篇关于哪些事情在case类将被删除后Scala 2.9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!