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

Paul Cantrell cantrell at pobox.com
Sun Dec 13 22:24:33 CST 2015


> On Dec 13, 2015, at 9: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.

Speak of the devil:

https://github.com/bustoutsolutions/siesta/blob/master/Source/Support/WeakCache.swift#L52
https://github.com/bustoutsolutions/siesta/blob/master/Source/ResourceObserver.swift#L299-L300

Both of these spots leave zombie entires in collections when weakly referenced objects go away (cached resources in the first link, observer owners in second). Siesta gets away with it only because a low memory event triggers a cleanup.

So yeah, this would be useful in the wild.

Not sure about this, but … perhaps Swift could get away with providing this only for properties and not for local variables? If so, might this fold into Joe Groff’s forthcoming property decoration mechanism?

Cheers, P

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
https://innig.net • @inthehands • http://siestaframework.com/




More information about the swift-evolution mailing list