[swift-evolution] Proposal: Weak Native Swift Containers

Greg Parker gparker at apple.com
Thu Dec 10 18:53:25 CST 2015

> On Dec 10, 2015, at 2:55 PM, Riley Testut via swift-evolution <swift-evolution at swift.org> wrote:
> I think native swift collections with support for weak references for their contents would be very useful, and in more places than just listed above. I don’t think Array could be easily extended to support it (what happens if a value is released? does everything shift down? do they keep their indices?), but Set and Dictionary (where the keys and/or values could be weak, akin to NSMapTable) would be good candidates IMO.

Containers of weak references are valuable, but I suspect they will need to be completely distinct from Array and Set and Dictionary. The problem is that a weak reference can become nil at any time. This subverts the value semantics of Swift's current containers and defeats assumptions that typical container algorithms make. (For example, a typical sorting algorithm assumes that comparison results between two elements are consistent over time, but that isn't true if a weak reference disappears while a sort is in progress.)

Note that Foundation does not support weak references in NSDictionary or NSSet.

Greg Parker     gparker at apple.com     Runtime Wrangler

More information about the swift-evolution mailing list