score:6

Accepted answer

Simply:

val originalA: A = // ...
val update: Update = // ...
val newA: A = A(
  id = originalA.id,
  a = update.a.getOrElse(originalA.a),
  ...
)

score:7

All case classes have a copy method.

http://docs.scala-lang.org/tutorials/tour/case-classes.html (find the copy term)

val a = A(1, "", "", "")
val update = Update(None, "scalaz".some, None)
val b = a.copy(
  b = update.b.getOrElse(a.b)
)

Also check out the lens pattern for copying deeply nested objects in a functional manner:

http://eed3si9n.com/learning-scalaz/Lens.html

Once you have objects made up of other objects and so on, it becomes highly cumbersome to use the copy method. Scalaz's lens pattern implementation is a great alternative.


Related Query

More Query from same tag