What are the disadvantages to declaring Scala case classes?

One big disadvantage: case classes can't be extended via subclassing Other advantages you missed, listed for completeness: compliant serialization/deserialization, no need to use "new" keyword to create I prefer non-case classes for objects with mutable state, private state, or no state (e.g. Most singleton components). Case classes for pretty much everything else.

One big disadvantage: case classes can't be extended via subclassing. Other advantages you missed, listed for completeness: compliant serialization/deserialization, no need to use "new" keyword to create. I prefer non-case classes for objects with mutable state, private state, or no state (e.g. Most singleton components).

Case classes for pretty much everything else.

3 You can subclass a case class. The subclass can't be a case class too — that's the restriction. – Seth Tisue Jan 12 at 14:37.

First the good bits: Everything immutable by default Yes, and can even be overridden (using var) if you need it Getters automatically defined Possible in any class by prefixing params with val Decent toString() implementation Yes, very useful, but doable by hand on any class if necessary Compliant equals() and hashCode() Combined with easy pattern-matching, this is the main reason that people use case classes Companion object with unapply() method for matching Also possible to do by hand on any class by using extractors This list should also include the uber-powerful copy method, one of the best things to come to Scala 2.8 Then the bad, there are only a handful of real restrictions with case classes: You can't define apply in the companion object using the same signature as the compiler-generated method In practice though, this is rarely a problem. Changing behaviour of the generated apply method is guaranteed to surprise users and should be strongly discouraged, the only justification for doing so is to validate input parameters - a task best done in the main constructor body (which also makes the validation available when using copy) You can't subclass True, though it's still possible for a case class to itself be a descendant. One common pattern is to build up a class hierarchy of traits, using case classes as the leaf nodes of the tree.It's also worth noting the sealed modifier.

Any subclass of a trait with this modifier must be declared in the same file. When pattern-matching against instances of the trait, the compiler can then warn you if you haven't checked for all possible concrete subclasses. When combined with case classes this can offer you a very high level level of confidence in your code if it compiles without warning.

As a subclass of Product, case classes can't have more than 22 parameters No real workaround, except to stop abusing classes with this many params :) Also... One other restriction sometimes noted is that Scala doesn't (currently) support lazy params (like lazy vals, but as parameters). The workaround to this is to use a by-name param and assign it to a lazy val in the constructor. Unfortunately, by-name params don't mix with pattern matching, which prevents the technique being used with case classes as it breaks the compiler-generated extractor.

This is relevant if you want to implement highly-functional lazy data structures, and will hopefully be resolved with the addition of lazy params to a future release of Scala.

Thanks for the comprehensive answer. I think everything exception "You can't subclass" is probably unlikely to phase me anytime soon. – Graham Lea Jan 12 at 10:18 You can subclass a case class.

The subclass can't be a case class too — that's the restriction. – Seth Tisue Jan 12 at 14:38.

Jim, In fact, case class can have companion object, Scala is smart enough to handle it. The following is the example. Case class MyCaseClass(x: Int) { def isInList = MyCaseClass.

List contains x } object MyCaseClass { private val list = List(1, 3, 5, 7, 9) // Factory method def apply(x: Float): MyCaseClass = MyCaseClass(x. ToInt) } val x = MyCaseClass(1) val y = MyCaseClass(10) println (x. IsInList) println (y.

IsInList).

This answer is completely off-topic. You should just have commented on Jim's answer. – Daniel C.

Sobral Jan 11 at 21:49.

I think the TDD principle apply here: do not over-design. When you declare something to be a case class, you are declaring a lot of functionality. That will decrease the flexibility you have in changing the class in the future.

For example, a case class has an equals method over the constructor parameters. You may not care about that when you first write your class, but, latter, may decide you want equality to ignore some of these parameters, or do something a bit different. However, client code may be written in the mean time that depends on case class equality.

I don't think client code should depend on the exact meaning of 'equals'; it is up to a class to decide what 'equals' means to it. The class author should be free to change the implementation of 'equals' down the line. – pkaeding Feb 1 at 6:16 @pkaeding You are free to not have client code depend on any private method.

Everything that is public is a contract you have agreed to. – Daniel C. Sobral Feb 1 at 14:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions