Scala 2.9 后究竟会删除哪些围绕案例类的内容?

发布于 11-07 05:47 字数 1015 浏览 4 评论 0原文

我知道计划对案例类进行一些更改,例如禁止它们之间的继承:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Scala 中的每个类都必须至少有一个非隐式参数部分。如果您不包含其中之一,编译器将为您添加它。

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

案例类也不例外。但与模式匹配的交互是混乱和错误的根源,这促使了弃用。

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

正如 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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文