更新嵌套结构的更清洁方法
说我有以下两个case classes:
case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, address: Address)
和以下Person类的实例:
val raj = Person("Raj", "Shekhar", Address("M Gandhi Marg",
"Mumbai",
"Maharashtra",
411342))
现在,如果我想更新zipCode,raj那么我将不得不做:
val updatedRaj = raj.copy(address = raj.address.copy(zipCode = raj.address.zipCode + 1))
随着嵌套水平的提高,这将变得更加丑陋。是否有更清洁的方式(像Clojure的东西update-in)来更新这样的嵌套结构?
MYYA
守候你守候我