为什么不推荐使用没有参数列表的案例类?

发布于 2024-08-21 06:56:03 字数 123 浏览 9 评论 0原文

为什么 Scala 不推荐使用没有参数列表的案例类?为什么编译器建议使用 () 作为参数列表?

编辑:

请有人回答我的第二个问题...:|

Why were the case classes without a parameter list deprecated from Scala? And why does compiler suggest to use () as parameter list instead?

EDIT :

Someone please answer my second question... :|

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

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

发布评论

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

评论(2

无敌元气妹 2024-08-28 06:56:03

很容易意外地错误地使用无参数案例类作为模式。

scala> case class Foo                                             
warning: there were deprecation warnings; re-run with -deprecation for details
defined class Foo

scala> (new Foo: Any) match { case Foo => true; case _ => false } 
res10: Boolean = false

而不是:

scala> (new Foo: Any) match { case _: Foo => true; case _ => false } 
res11: Boolean = true

或者更好:

scala> case object Bar                                               
defined module Bar

scala> (Bar: Any) match { case Bar => true; case _ => false }        
res12: Boolean = true

更新希望下面的文字记录能够演示为什么空参数列表比已弃用的缺失参数列表更受欢迎。

scala> case class Foo() // Using an empty parameter list rather than zero parameter lists.
defined class Foo

scala> Foo // Access the companion object Foo
res0: Foo.type = <function0>

scala> Foo() // Call Foo.apply() to construct an instance of class Foo
res1: Foo = Foo()

scala> case class Bar
warning: there were deprecation warnings; re-run with -deprecation for details
defined class Bar

scala> Bar // You may expect this to construct a new instance of class Bar, but instead
           // it references the companion object Bar 
res2: Bar.type = <function0>

scala> Bar() // This calls Bar.apply(), but is not symmetrical with the class definition.
res3: Bar = Bar()

scala> Bar.apply // Another way to call Bar.apply
res4: Bar = Bar()

案例对象通常仍然优于空参数列表。

It is really easy to accidentally use a no-arg case class incorrectly as a pattern.

scala> case class Foo                                             
warning: there were deprecation warnings; re-run with -deprecation for details
defined class Foo

scala> (new Foo: Any) match { case Foo => true; case _ => false } 
res10: Boolean = false

Instead of:

scala> (new Foo: Any) match { case _: Foo => true; case _ => false } 
res11: Boolean = true

Or better:

scala> case object Bar                                               
defined module Bar

scala> (Bar: Any) match { case Bar => true; case _ => false }        
res12: Boolean = true

UPDATE Hopefully the transcript below will demonstrate why an empty parameter list is preferred to the deprecated missing parameter list.

scala> case class Foo() // Using an empty parameter list rather than zero parameter lists.
defined class Foo

scala> Foo // Access the companion object Foo
res0: Foo.type = <function0>

scala> Foo() // Call Foo.apply() to construct an instance of class Foo
res1: Foo = Foo()

scala> case class Bar
warning: there were deprecation warnings; re-run with -deprecation for details
defined class Bar

scala> Bar // You may expect this to construct a new instance of class Bar, but instead
           // it references the companion object Bar 
res2: Bar.type = <function0>

scala> Bar() // This calls Bar.apply(), but is not symmetrical with the class definition.
res3: Bar = Bar()

scala> Bar.apply // Another way to call Bar.apply
res4: Bar = Bar()

A case object would usually still be preferred over an empty parameter list.

月亮邮递员 2024-08-28 06:56:03

如果没有参数,案例类的每个实例都是无法区分的,因此本质上是一个常量。针对这种情况使用一个对象。

Without parameters, every instance of the case class is indistinguishable and hence is essentially a constant. Use an object for that case.

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