Created
March 24, 2016 20:30
-
-
Save keybuk/d58286b4a422ad9a16a1 to your computer and use it in GitHub Desktop.
Withable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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