最佳答案
Say I have got following two case class
es:
case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, address: Address)
and the following instance of Person
class:
val raj = Person("Raj", "Shekhar", Address("M Gandhi Marg",
"Mumbai",
"Maharashtra",
411342))
Now if I want to update zipCode
of raj
then I will have to do:
val updatedRaj = raj.copy(address = raj.address.copy(zipCode = raj.address.zipCode + 1))
With more levels of nesting this gets even more uglier. Is there a cleaner way (something like Clojure's update-in
) to update such nested structures?