IMHO it could well be a goal. Expanding @LLC example:
struct Person {
let first: String
let last: String
let age: Int
init?(first: String, last: String, age: Int) {
guard isValidFirstName(first) else { return nil }
guard isValidLastName(first) else { return nil }
guard isValidAge(age) else { return nil }
self.first = first
self.last = last
self.age = age
}
func with(first: String? = nil, last: String? = nil, age: Int? = nil) -> Self? {
Self.init(
first: first ?? self.first,
last: first ?? self.first,
age: age ?? self.age
)
}
}
With enough fields writing this "with" manually results in quite large amount of boilerplate, besides it's error prone (there's a typo in the above implementation) and doesn't support overriding optional fields, something that built-in "with" could have provided:
let clonedPerson = person.with(
first: "another",
// last is not provided → taken from person.last
address: nil // provided → overrides address with nil
)