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
27
votes
1 answer
How to define schema for custom type in Spark SQL?
The following example code tries to put some case objects into a dataframe. The code includes the definition of a case object hierarchy and a case class using this trait:
import org.apache.spark.{SparkContext, SparkConf}
import…

Martin Senne
- 5,939
- 6
- 30
- 47
21
votes
2 answers
Scala: Case class unapply vs a manual implementation and type erasure
I'm trying to understand what Scala does with Case Classes that makes them somehow immune to type erasure warnings.
Let's say we have the following, simple class structure. It's basically an Either:
abstract class BlackOrWhite[A, B]
case class…

Nycto
- 1,670
- 2
- 14
- 18
20
votes
2 answers
Scala: order of definition for companion object vs case class
In Scala 2.9.1 I get the following behavior:
class Foo {
case class X()
object X // this compiles
def bar() {
object Y // this compiles
case class Y()
case class Z()
object Z // won't…

Gregor Scheidt
- 3,952
- 4
- 24
- 28
19
votes
1 answer
Extract label values from a LabelledGeneric instance
Consider the following example:
import shapeless._
case class Foo(bar: String, baz: Boolean)
val labl = LabelledGeneric[Foo]
Now, the type of labl is (prettified)
LabelledGeneric[Foo] {
type Repr =
FieldType[Symbol @@ String("bar"), String]…

Gabriele Petronella
- 106,943
- 21
- 217
- 235
19
votes
2 answers
Using generic case classes in Scala
I wonder whether using generics for Scala case classes can save some boilerplate code.
Let's save I have the following class hieararchy to emulate a "variant" type that boxes a set of types and allows unboxing them using pattern matching:
sealed…

Alexey Alexandrov
- 2,951
- 1
- 24
- 24
19
votes
3 answers
How to use a case classes when hierarchy is needed?
I know that you're not allowed to inherit from case classes but how would you do when you really need to? We have two classes in a hierarchy, both contain many fields, and we need to be able to create instances of both. Here's my options:
If I'd…

uzilan
- 2,554
- 2
- 31
- 46
17
votes
3 answers
rely on methods of case class in trait
Is there a way to rely on methods defined in case class in a trait? E.g., copy: the following doesn't work. I'm not sure why, though.
trait K[T <: K[T]] {
val x: String
val y: String
def m: T = copy(x = "hello")
def copy(x: String = this.x,…

Aaron Yodaiken
- 19,163
- 32
- 103
- 184
17
votes
2 answers
What is the Scala case class equivalent in PySpark?
How would you go about employing and/or implementing a case class equivalent in PySpark?

conner.xyz
- 6,273
- 8
- 39
- 65
17
votes
2 answers
Can I get a Scala case class definition from an Avro schema definition?
To facilitate working with Avro in Scala, I'd like to define a case class based on the schema stored with a .avro file. I could try:
Writing a .scala case class definition by hand.
Programmatically writing strings to a .scala file
Spoof the…

Julian Peeters
- 853
- 1
- 6
- 19
15
votes
2 answers
Scala: Mix traits and case class in pattern match
I want to match on some case classes. If I don't know them, I want to match on a specified trait the classes have to extend. This looks like
trait Event //root trait
trait Status extends Event //special trait
trait UIEvent extends Event //special…

Muki
- 3,513
- 3
- 27
- 50
15
votes
1 answer
Case class equality in Apache Spark
Why does pattern matching in Spark not work the same as in Scala? See example below... function f() tries to pattern match on class, which works in the Scala REPL but fails in Spark and results in all "???". f2() is a workaround that gets the…

kmh
- 1,516
- 17
- 33
15
votes
3 answers
How to avoid scala's case class default toString function being overridden?
Scala case class has a default toString function. But when this case class extends a trait with an existing toString() function, it will be rendered useless. How can I prevent this situation?

tribbloid
- 4,026
- 14
- 64
- 103
14
votes
3 answers
Case classes, pattern matching and curried constructors in Scala
They don't seem to mix that well:
abstract class A
case class B (var a: Int)(var b: String) extends A
case class C extends A
The following will not work:
B(1)("1") match {
case B(a)(b) => print("B")
case C() => print("C")
}
The problem is that…

Henry Henrinson
- 5,203
- 7
- 44
- 76
14
votes
1 answer
Case class constructor argument type depending on the previous argument value
I am trying to do the following
trait Stateful {
type State
}
case class SystemState(system: Stateful, state: system.State) // does not compile
That is, the type of statedepends on (the value of) system. That, however, is not supported:
illegal…

Tomas Mikula
- 6,537
- 25
- 39
14
votes
1 answer
"dynamically" creating case classes with macros
I would like to create a macro generated hierarchy of sealed abstract and case classes. There was an example similar to this with http://docs.scala-lang.org/overviews/macros/typemacros.html but is is now obsolete. Is this still possible?
I think…

user833970
- 2,729
- 3
- 26
- 41