I'm fairly new to Scala and I have a question about the best way to copy a case class while preserving data that comes from traits. For example, let's say I have the following:
trait Auditing {
var createTime: Timestamp = new Timestamp(System.currentTimeMillis)
}
case class User(val userName: String, val email: String) extends Auditing
val user = User("Joe", "[email protected]")
Then I want to make a new copy with one parameter changed:
val user2 = user.copy(email = "[email protected]")
Now, in the example above, the property createTime does not get copied over because it is not defined in the constructor of the User case class. So my question is: assuming that moving createTime into the constructor is not an option, what is the best way for getting a copy of the User object that includes the value from the trait?
I'm using Scala 2.9.1
Thanks in advance! Joe
User
case class or you use Scala 2.10 macros feature to automate that. The second option definitely won't be an easy task for a beginner. - Nikita VolkovToolbox
api. The accepted answer there is based on macros, but I think it doesn't support the latest Scala version. - Nikita Volkov