I have the following class:
class Client {
let name: String
let age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
let wrongClient = Client(name: "John", age: 9)
How to create a new version wrongClientwith the right age?
I need something like the following:
let rightClient = Client(wrongClient, age: 42)
For example, OCaml allows developers to do the following:
type client = {
name : string;
age : int;
}
let wrong_client = {name = "John"; age = 25}
let right_client = {wrong_client with age = 42}
Or in Scala:
case class Client(name: String, age: Int)
val wrongClient = Client(name: "John", age: 9)
val rightClient = wrongClient.copy(age=42)
EDIT
I want to experiment with data immutability and data exchange using Swift.
Because immutable data means “generating” values from other values, “copying” objects can occur frequently. So my question is: how can I easily create new objects from other objects using Swift?
EDIT 2
I'm looking at Swiftz lenses right now .
source
share