Skip to content

Instantly share code, notes, and snippets.

@rintaro
Created May 19, 2017 07:46
Show Gist options
  • Save rintaro/1668e46bc969c5f991079dd5bf530dd1 to your computer and use it in GitHub Desktop.
Save rintaro/1668e46bc969c5f991079dd5bf530dd1 to your computer and use it in GitHub Desktop.
protocol Resettable {
mutating func reset()
init()
}
extension Resettable {
mutating func reset() {
self = Self() // Assigning to 'self'.
}
}
class Foo : Resettable {
var value: Int
required init() {
value = 0
}
}
var ref1 = Foo()
let ref2 = ref1
assert(ObjectIdentifier(ref1) == ObjectIdentifier(ref2))
ref1.value = 42
assert(ObjectIdentifier(ref1) == ObjectIdentifier(ref2))
ref1.reset()
assert(ObjectIdentifier(ref1) != ObjectIdentifier(ref2))
assert(ref1.value == 0)
assert(ref2.value == 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment