Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which things around case classes will be removed after Scala 2.9 exactly?

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?

like image 517
soc Avatar asked May 14 '11 15:05

soc


People also ask

What is the difference between Case class and class in Scala?

A class can extend another class, whereas a case class can not extend another case class (because it would not be possible to correctly implement their equality).

What does case class do in Scala?

What is Scala Case Class? A Scala Case Class is like a regular class, except it is good for modeling immutable data. It also serves useful in pattern matching, such a class has a default apply() method which handles object construction. A scala case class also has all vals, which means they are immutable.

What is difference between Case class and case object in Scala?

A case object is like an object , but just like a case class has more features than a regular class, a case object has more features than a regular object. Its features include: It's serializable. It has a default hashCode implementation.

Can you extend a case class in Scala?

To extend a class in Scala we use extends keyword. there are two restrictions to extend a class in Scala : To override method in scala override keyword is required. Only the primary constructor can pass parameters to the base constructor.


1 Answers

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?

like image 161
retronym Avatar answered Oct 04 '22 12:10

retronym