7
更新嵌套结构的更干净方法
说我有以下两个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)来更新此类嵌套结构?
124
scala
case-class
zipper