Skip to content

Instantly share code, notes, and snippets.

@0111b
Created October 19, 2020 21:03
Show Gist options
  • Save 0111b/a0dbc1e61529a3b4dd2820b921290100 to your computer and use it in GitHub Desktop.
Save 0111b/a0dbc1e61529a3b4dd2820b921290100 to your computer and use it in GitHub Desktop.
/// Adds a helper function to mutate a properties and help implement _Builder_ pattern
protocol Buildable { }
extension Buildable {
/// Mutates a property of the instance
///
/// - Parameter keyPath: `WritableKeyPath` to the instance property to be modified
/// - Parameter value: value to overwrite the instance property
func mutating<T>(keyPath: WritableKeyPath<Self, T>, value: T) -> Self {
var newSelf = self
newSelf[keyPath: keyPath] = value
return newSelf
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment