Skip to content

Instantly share code, notes, and snippets.

@keybuk
Created March 24, 2016 20:30
Show Gist options
  • Save keybuk/d58286b4a422ad9a16a1 to your computer and use it in GitHub Desktop.
Save keybuk/d58286b4a422ad9a16a1 to your computer and use it in GitHub Desktop.
Withable
struct Person {
var firstName, lastName: String
}
let john = Person(firstName: "John", lastName: "Doe")
protocol Withable {
func with(changes: (inout Self) -> Void) -> Self
}
extension Withable {
func with(changes: (inout Self) -> Void) -> Self {
var this = self
changes(&this)
return this
}
}
extension Person: Withable {}
let carol = john.with {
$0.firstName = "Carol"
}
print(carol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment