Skip to content

Instantly share code, notes, and snippets.

@achernoprudov
Created December 25, 2020 07:37
Show Gist options
  • Save achernoprudov/5997689e2007d84b48236199d0574db7 to your computer and use it in GitHub Desktop.
Save achernoprudov/5997689e2007d84b48236199d0574db7 to your computer and use it in GitHub Desktop.
/// Utility protocol for cloning objects.
public protocol Clonable {
}
public extension Clonable {
/// Clones object with modifictation:
///
/// Example:
/// ```
/// let newUser = user.clone {
/// $0.name = "John"
/// $0.phone = "+1 978 873 88 19"
/// }
/// ```
///
/// - Parameter modificationBlock: modification applied to clonned object
/// - Returns: new object with modified field
func clone(with modificationBlock: ((inout Self) -> Void)? = nil) -> Self {
var result = self
modificationBlock?(&result)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment