Created
December 25, 2020 07:37
-
-
Save achernoprudov/5997689e2007d84b48236199d0574db7 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
| /// 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