Created
February 4, 2020 23:11
-
-
Save noahsark769/e83f0dc30aced4e805df5cacdb086dd5 to your computer and use it in GitHub Desktop.
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
protocol KeyPathUpdatable {} | |
extension KeyPathUpdatable { | |
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self { | |
var copy = self | |
copy[keyPath: keyPath] = value | |
return copy | |
} | |
} | |
struct Book: KeyPathUpdatable { | |
var title: String | |
var author: String | |
var isbn: Int | |
var publishedYear: Int | |
} | |
let book = Book(title: "Something", author: "Noah", isbn: 1, publishedYear: 2020) | |
let updated = book.updating(\.title, to: "Else") | |
print(book) | |
print(updated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment