Skip to content

Instantly share code, notes, and snippets.

@dydus0x14
Last active October 27, 2018 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dydus0x14/6fbfabcbf077e7d84ff04ca98b5cd791 to your computer and use it in GitHub Desktop.
Save dydus0x14/6fbfabcbf077e7d84ff04ca98b5cd791 to your computer and use it in GitHub Desktop.
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