I had a couple of case classes like that:
case class Size(id: Long, size: Int) case class Title(id: Long, title: String) . . . I had like 10 of these with pretty much the same functions. I decided to combine them under a common trait, which led to something like this:
trait Property[T]{ val value: T } trait PropertyPair[T]{ val id: Long val prop: Property[T] } case class Size(id: Long, prop: Property[Int]) extends PropertyPair[Int] { def size: Int = prop.value } case class Title(id: Long, prop: Property[String]) extends PropertyPair[String] { def title: String = prop.value } Though the code block above seems to be a good solution, and now I can actually define functions under the PropertyPair trait, but the code still smells.
- I need to include
[T]three times for every property I add - I need to explicitly add property name as an extra function to access it as though it is just a field in the case class.
Now to initialize Title i need to write
Title(101L, Property("Title")) instead of
Title(101L, "Title") For some reason I am sure there is a much more elegant and less error-prone solution then the one I provided.