Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching.
Questions tagged [case-class]
711 questions
491
votes
17 answers
What is the difference between Scala's case class and class?
I searched in Google to find the differences between a case class and a class. Everyone mentions that when you want to do pattern matching on the class, use case class. Otherwise use classes and also mentioning some extra perks like equals and hash…

Teja Kantamneni
- 17,402
- 12
- 56
- 86
251
votes
14 answers
Case objects vs Enumerations in Scala
Are there any best-practice guidelines on when to use case classes (or case objects) vs extending Enumeration in Scala?
They seem to offer some of the same benefits.

Alex Miller
- 69,183
- 25
- 122
- 167
155
votes
1 answer
IntelliJ Scala Plugin's case class indentation is absurd
When a case class has many fields and their names are long, it is often a good idea to write each field in each line like:
case class Person (
name: String,
age: Int
)
This resembles C/C++ struct definition and totally readable even when the…

lyomi
- 4,230
- 6
- 30
- 39
128
votes
7 answers
Cleaner way to update nested structures
Say I have got following two case classes:
case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, address: Address)
and the following instance of Person class:
val raj =…

missingfaktor
- 90,905
- 62
- 285
- 365
119
votes
7 answers
Easy idiomatic way to define Ordering for a simple case class
I have a list of simple scala case class instances and I want to print them in predictable, lexicographical order using list.sorted, but receive "No implicit Ordering defined for ...".
Is there exist an implicit that provides lexicographical…

ya_pulser
- 2,620
- 2
- 16
- 21
112
votes
2 answers
Overload constructor for Scala's Case Classes?
In Scala 2.8 is there a way to overload constructors of a case class?
If yes, please put a snippet to explain, if not, please explain why?

Felix
- 8,385
- 10
- 40
- 59
107
votes
5 answers
What are the disadvantages to declaring Scala case classes?
If you're writing code that's using lots of beautiful, immutable data structures, case classes appear to be a godsend, giving you all of the following for free with just one keyword:
Everything immutable by default
Getters automatically…

Graham Lea
- 5,797
- 3
- 40
- 55
99
votes
4 answers
Scala case class inheritance
I have an application based on Squeryl. I define my models as case classes, mostly since I find convenient to have copy methods.
I have two models that are strictly related. The fields are the same, many operations are in common, and they are to be…

Andrea
- 20,253
- 23
- 114
- 183
88
votes
10 answers
How to override apply in a case class companion
So here's the situation. I want to define a case class like so:
case class A(val s: String)
and I want to define an object to ensure that when I create instances of the class, the value for 's' is always uppercase, like so:
object A {
def…

John S
- 1,695
- 2
- 14
- 25
78
votes
12 answers
Case class to map in Scala
Is there a nice way I can convert a Scala case class instance, e.g.
case class MyClass(param1: String, param2: String)
val x = MyClass("hello", "world")
into a mapping of some kind, e.g.
getCCParams(x) returns "param1" -> "hello", "param2" ->…

Will
- 1,711
- 1
- 12
- 17
74
votes
2 answers
What is *so* wrong with case class inheritance?
While looking for something else, quite out of mere coincidence I stumbled upon few comments about how diabolical case class inheritance is. There was this thing called ProductN , wretches and kings, elves and wizards and how some kind of a very…

Ashkan Kh. Nazary
- 21,844
- 13
- 44
- 68
65
votes
5 answers
In Scala, is there an easy way to convert a case class into a tuple?
Is there an easy way to convert a case class into a tuple?
I can, of course, easily write boilerplate code to do this, but I mean without the boilerplate.
What I'm really after is a way to easily make a case class lexicographically Ordered. I can…

Douglas
- 2,555
- 3
- 24
- 30
62
votes
1 answer
Should I use the final modifier when declaring case classes?
According to scala-wartremover static analysis tool I have to put "final" in front of every case classes I create: error message says "case classes must be final".
According to scapegoat (another static analysis tool for Scala) instead I shouldn't…

sscarduzio
- 5,938
- 5
- 42
- 54
61
votes
4 answers
hashCode in case classes in Scala
I've read that Scala'a case class construct automatically generates a fitting equals and hashCode implementation. What does exactly the generated code look like?
user735703
60
votes
11 answers
Scala - how to print case classes like (pretty printed) tree
I'm making a parser with Scala Combinators. It is awesome. What I end up with is a long list of entagled case classes, like: ClassDecl(Complex,List(VarDecl(Real,float), VarDecl(Imag,float))), just 100x longer. I was wondering if there is a good way…

kornfridge
- 5,102
- 6
- 28
- 40