I have a case class (let's name it Stuff
) that I want to be able to create anonymous subclasses of at run time by extending a trait (call it Marker
). Here's a snippet of a REPL session that illustrates what I'm trying to do:
scala> trait Marker
defined trait Marker
scala> case class Stuff(i: Int)
defined class Stuff
scala> val a = Stuff(1)
a: Stuff = Stuff(1)
scala> val b = new Stuff(1) with Marker
b: Stuff with Marker = Stuff(1)
Note how a
is instantiated using Stuff.apply()
, while in b
's case I'm calling the case class' constructor.
My question is: is instantiating case classes using a constructor kosher? It appears to me that it is, since the usual conveniences provided by case classes, such as ==
, .equals()
, and .hashCode()
, all work. Am I missing anything that would brand what I'm doing a Bad Thing (TM)?
scala> a == b
res0: Boolean = true
scala> a.equals(b)
res1: Boolean = true
scala> a.hashCode == b.hashCode
res2: Boolean = true