-
-
Save unnamedd/63842053c2a764e03ccde6647e068e68 to your computer and use it in GitHub Desktop.
This file contains hidden or 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