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?
Perhaps QuickLens matches your question better. QuickLens uses macro's to convert an IDE friendly expression into something that is close to the original copy statement.
Given the two example case classes:
and the instance of Person class:
you can update zipCode of raj with: