I'm using Play2+Scala+ReactiveMongo to build a web application. Since mongodb
doesn't require all documents to have the same structure, I make a large use of case classes with Options
as parameters to implement models. For example:
case class PersonInfo(address: Option[String],
telephone: Option[String],
age: Option[Int],
job: Option[String])
case class Person(id: UUID, name: String, info: PersonInfo)
Now suppose I want to merge two PersonInfo
objects, for example in a update function. I do right now is:
val updPInfo = old.copy(address = new.address orElse address,
telephone = new.telephone orElse telephone,
age = new.age orElse age,
job = new.job orElse job)
This way I have an object that has new values where they were specified by the new
object and old values for the remaining ones.
This actually works fine, but it is bit ugly to see when the list of parameters grows.
Is there a more elegant way to do that?