[swift-evolution] Proposal: Give weak references the ability to notify reference-holders when they transition to nil
jordan_rose at apple.com
Tue Dec 15 12:29:16 CST 2015
> On Dec 14, 2015, at 11:54 , John McCall via swift-evolution <swift-evolution at swift.org> wrote:
>> On Dec 13, 2015, at 7:46 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>>> On Dec 13, 2015, at 6:24 PM, Michael Henson via swift-evolution <swift-evolution at swift.org> wrote:
>>> The use-case for this comes first from proposals to have a weak-reference version of collection types. Implementing a notification signal of some sort to weak reference-holders when the reference becomes nil would make implementing those more straightforward.
>> +1. This is very useful for various kinds of APIs, like a weak hashtable that wants to remove the keys when/if they get deallocated.
> Yes. This was always part of the long-term vision for weak references.
Hm. I don't know. Allowing arbitrary callbacks during the deinit process is a little scary to me; it means that any API that vends an object publicly can no longer assume that deinitialization is cheap. I mean, deinit can already do arbitrary work, but the creator of the class has full control over what that work is (modulo their superclasses). This feels like something that a class might need to opt into, much like KVO requires opt-in in Swift with 'dynamic'.
If one of the selling points of ARC over GC is "deterministic destruction", there's value in being able to control what happens during that destruction.
More information about the swift-evolution