If you have a case class like:
case class Foo(x: String, y: String, z: String)
And you have two instances like:
Foo("x1","y1","z1")
Foo("x2","y2","z2")
Is it possible to merge instance 1 in instance 2, except for field z, so that the result would be:
Foo("x1","y1","z2")
My usecase is just that I give JSON objects to a Backbone app through a Scala API, and the Backbone app gives me back a JSON of the same structure so that I can save/update it. These JSON objects are parsed as case class for easy Scala manipulation. But some fields should never be updated by the client side (like creationDate). For now I'm doing a manual merge but I'd like a more generic solution, a bit like an enhanced copy function.
What I'd like is something like this:
instanceFromDB.updateWith(instanceFromBackbone, excludeFields = "creationDate" )
But I'd like it to be typesafe :)
Edit: My case class have a lot more fields and I'd like the default bevavior to merge fields unless I explicitly say to not merge them.
z, so you copy it from instance 1 to instance 2. Then you have exactly everything in instance 2 by default, except for what you explicitly copy over from 1. (It's all immutable so the new thing will be neither 1 nor 2 anyway; it doesn't matter who you start with.) The only case where this wouldn't work is where the two instances aren't actually the same case class. - Rex KerrinstanceFromDB.updateWithIt's not robust to exclude some fields, you had some long list of values to update, but now ended up with kind of opposite logic (small list of values you don't update). When you add new fields you also must check that small list, otherwise client will overwrite them, and that's a serious error. If a client does not update some field - rather marginal bug. - idonnie