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
55
votes
6 answers
How to get around the Scala case class limit of 22 fields?
Scala case classes have a limit of 22 fields in the constructor. I want to exceed this limit, is there a way to do it with inheritance or composition that works with case classes?

Phil
- 46,436
- 33
- 110
- 175
48
votes
3 answers
Should exceptions be case classes?
Should my custom exception types be case classes?
On the plus side, I get extractors.
On the minus side, I get incorrect equality semantics. But I can avoid that by overriding equals.
So does it make sense, at a conceptual level, to make them…

missingfaktor
- 90,905
- 62
- 285
- 365
44
votes
4 answers
Using .tupled method when companion object is in class
I am in the process of migrating from Slick to Slick 2, and in Slick 2 you are meant to use the tupled method when projecting onto a case class (as shown here http://slick.typesafe.com/doc/2.0.0-RC1/migration.html)
The problem is when the case class…

mdedetrich
- 1,899
- 1
- 18
- 29
42
votes
2 answers
Why were the case classes without a parameter list deprecated?
Why were the case classes without a parameter list deprecated from Scala? And why does compiler suggest to use () as parameter list instead?
EDIT :
Someone please answer my second question... :|

missingfaktor
- 90,905
- 62
- 285
- 365
42
votes
4 answers
Scala case class private constructor but public apply method
If I have the following case class with a private constructor and I can not access the apply-method in the companion object.
case class Meter private (m: Int)
val m = Meter(10) // constructor Meter in class Meter cannot be accessed...
Is there a…

Erik
- 2,888
- 2
- 18
- 35
40
votes
7 answers
Scala: Parse JSON directly into a case class
Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box.
What about if the JSON should…

SRobertJames
- 8,210
- 14
- 60
- 107
39
votes
3 answers
Scala case class extending Product with Serializable
I am learning scala and tried following form Scala Cookbook:
trait Animal
trait FurryAnimal extends Animal
case class Dog(name:String) extends Animal
case class Cat(name:String) extends Animal
Now when I did following as :
val x =…

optional
- 3,260
- 5
- 26
- 47
38
votes
4 answers
Why do case class companion objects extend FunctionN?
When you create a case class, the compiler creates a corresponding companion object with a few of the case class goodies: an apply factory method matching the primary constructor, equals, hashCode, and copy.
Somewhat oddly, this generated object…

retronym
- 54,768
- 12
- 155
- 168
37
votes
5 answers
Mixing in a trait dynamically
Having a trait
trait Persisted {
def id: Long
}
how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in?
The signature of the method looks like:
def toPersisted[T](instance: T, id:…

Nikita Volkov
- 42,792
- 11
- 94
- 169
36
votes
3 answers
How do I "get" a Scala case object from Java?
I created a hierarchy of case objects in Scala that looks like the following:
package my.awesome.package
sealed abstract class PresetShapeType(val displayName: String)
case object AccelerationSensor extends PresetShapeType("Acceleration…

mipadi
- 398,885
- 90
- 523
- 479
34
votes
3 answers
Scala case class update value
I have a case class with 2 String members. I would like to update The second member later, so first I create an instance with String and None and later I load the data to the class and would like to update the second member with some value.
How can…

bashan
- 3,572
- 6
- 41
- 58
34
votes
3 answers
Scala Macros: Making a Map out of fields of a class in Scala
Let's say that I have a lot of similar data classes. Here's an example class User which is defined as follows:
case class User (name: String, age: Int, posts: List[String]) {
val numPosts: Int = posts.length
...
def foo = "bar"
...
}
I…

Emre
- 1,023
- 2
- 9
- 24
31
votes
2 answers
Why is parameter in contravariant position?
I'm trying to use a covariant type parameter inside a trait to construct a case-class like so:
trait MyTrait[+T] {
private case class MyClass(c: T)
}
compiler says:
error: covariant type T occurs in contravariant position in type T of value c
I…

lapislazuli
- 506
- 4
- 6
30
votes
2 answers
Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class
How can I extract the field values from a case class in scala using the new reflection model in scala 2.10?
For example, using the below doesn't pull out the field methods
def getMethods[T:TypeTag](t:T) = typeOf[T].members.collect {
case…

J Pullar
- 1,915
- 2
- 18
- 30
29
votes
2 answers
Constructing simple Scala case classes from Strings, strictly without boiler-plate
I seek succinct code to initialize simple Scala case classes from Strings (e.g. a csv line):
case class Person(name: String, age: Double)
case class Book(title: String, author: String, year: Int)
case class Country(name: String, population: Int,…

Perfect Tiling
- 661
- 4
- 13