[swift-evolution] Proposal: Give weak references the ability to notify reference-holders when they transition to nil

John McCall rjmccall at apple.com
Mon Dec 14 13:54:03 CST 2015


> 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.

>> SInce this is the germ of an idea, I'll avoid suggesting a syntax for the mechanism so the discussion focuses on whether or not this is useful enough as a feature to be implemented.
> 
> IMO, ideally, this would be more of a runtime API than a language feature.

We wouldn’t want to encumber every weak reference with the ability to support having callbacks dynamically registered on it.  So yeah, I think we’d want to provide some runtime functions that would be used to implement some stdlib API.

John.


More information about the swift-evolution mailing list