Skip to content

Instantly share code, notes, and snippets.

@V8tr
Created July 13, 2022 18:23
Show Gist options
  • Save V8tr/4eae6dcbf43471598ddad83572da3d7e to your computer and use it in GitHub Desktop.
Save V8tr/4eae6dcbf43471598ddad83572da3d7e to your computer and use it in GitHub Desktop.
KeyPath-based builder in Swift
protocol Buildable {}
extension Buildable {
func set<T>(_ keyPath: WritableKeyPath<Self, T>, to newValue: T) -> Self {
var copy = self
copy[keyPath: keyPath] = newValue
return copy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment