I would like to factor some common attributes of my case classes, say an id and a timestamp, to write generic code these. The following works fine, but I have to repeat all attributes on each case class:
trait HasIdAndTimestamp {
val id: Int
val timestamp: Long
}
case class Client(id: Int, timestamp: Long, name: String) extends HasIdAndTimestamp
case class Order(id: Int, timestamp: Long, items: List[Int], clientId: Int) extends HasIdAndTimestamp
...
Another option would be to have these common fields on a single field of type MetaData:
case class MetaData(id: Int, timestamp: Long)
trait HasMetaData {
val metadata: MetaData
}
case class Client(metadata: MetaData, name: String) extends HasMetaData
case class Order(metadata: MetaData, items: List[Int], clientId: Int) extends HasMetaData
...
But I don't really like to write .metadata.id all the time. Also I want to store these cases classes in a DB using Slick, and this nesting introduces a lot of additional boilerplate in the table definition. I would rather like something similar to the following:
case class MetaData(id: Int, timestamp: Long)
case class Client(name: String) extends MetaData
case class Order(items: List[Int], clientId: Int) extends MetaData
...
Where Client and Order both have these id and timestamp (val) field and apply/unapply defined accordingly.
Could shapeless/scalaz help me here?