Last active
October 27, 2018 12:30
-
-
Save dydus0x14/6fbfabcbf077e7d84ff04ca98b5cd791 to your computer and use it in GitHub Desktop.
This file contains 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 Copyable { | |
associatedtype V | |
func copy() -> V | |
func setup(v: V) -> V | |
} | |
class One: Copyable { | |
typealias V = One | |
var name: String? | |
func copy() -> V { | |
let instance = One() | |
return setup(instance) | |
} | |
func setup(v: V) -> V { | |
v.name = self.name | |
return v | |
} | |
} | |
class Two: One { | |
var id: Int? | |
override func copy() -> Two { | |
let instance = Two() | |
return setup(instance) | |
} | |
func setup(v: Two) -> Two { | |
super.setup(v) | |
v.id = self.id | |
return v | |
} | |
} | |
extension Array where Element: Copyable { | |
func clone() -> [Element.V] { | |
var copiedArray: [Element.V] = [] | |
for element in self { | |
copiedArray.append(element.copy()) | |
} | |
return copiedArray | |
} | |
} | |
let array = [One(), Two()] | |
let copied = array.clone() | |
print("\(array)") | |
print("\(copied)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment