7

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?

soc
  • 27,983
  • 20
  • 111
  • 215
  • 6
    The reason why case classes without parameter lists are "smelly" is because they carry no state that would be different from one instance to another, since they have no fields (at least none that are set through constructor arguments, which is the only way it should be done...), so they should really be case objects, since a single instance can represent all possible "states". – Dean Wampler May 14 '11 at 15:56
  • 5
    @Dean Wampler: How are case classes with an empty parameter list `()` different? – kassens May 14 '11 at 19:05
  • 3
    Why is inheritance between case classes smelly? – djondal Aug 21 '11 at 13:00

1 Answers1

8

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?

Community
  • 1
  • 1
retronym
  • 54,768
  • 12
  • 155
  • 168