为什么没有参数列表的情况下不推荐使用?

为什么没有参数列表的案例类从Scala弃用? 为什么编译器build议使用()作为参数列表呢?

编辑:

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

不小心使用无参数的case类作为模式真的很容易。

 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() 

一个case对象通常仍然比空的参数列表更受欢迎。

没有参数,case类的每个实例都是不可区分的,因此实质上是一个常量。 为这种情况使用一个对象。