# Can a group of weak references conform to Collection?

**URL:** <https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899>\
**Category:** Using Swift\
**Tags:** collection, weak\
**Created:** [June 29, 2023, 9:27pm UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899 "2023-06-29T21:27:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [June 29, 2023, 9:27pm UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899/1 "2023-06-29T21:27:47Z")

</div>

I have a type that collects weak references, which you can iterate over and append to. I want it to conform to `RangeReplaceableCollection`, but I don't think I can do anything more specific than `Sequence`.

The type itself is a classic pointer-length-capacity triplet, where the pointer is to a buffer of weak references. Once `length == capacity`, before trying to realloc, it scans the buffer for nils that it can drop out. It only performs a reallocation if it can't remove enough nils to make room for the new elements.

The problem with conforming it to `Collection` is, of all things, `subscript(_:)`. If the index type is just the index into the buffer, other threads could cause the weak reference to drop out from under you, and so indices could become invalid without apparent mutations to the collection.

I can think of two ways to fix this, neither of which is satisfying.

The first is to make the index type also hold a strong reference to the object. I guess this works, but I'm worried it's liable to produce footguns, when users don't realize they're holding a strong reference through the index.

The second is to make the element type `T?` instead of `T`. This is misleading since the `Iterator` skips nils, but it would make the subscript implementable.

The third option, I suppose, is to implement many (but not all) of the collection operations without actually conforming to the protocol. I'm worried this is what I'll have to do, unless I can justify one of the other two behaviors. Is there a way I can implement the protocol without changing the semantics of the type?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 29, 2023, 9:47pm UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899/2 "2023-06-29T21:47:42Z")

</div>

Not being a `Collection` seems like an inherent property of the data structure you're offering, not an implementation limitation. If the `n`th item in the sequence can change from `x` to `y` because `x` (or some other object earlier in the sequence) has been collected, then the sequence doesn't have stable indices.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 29, 2023, 10:13pm UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899/3 "2023-06-29T22:13:48Z")

</div>

I'd do your 2 or 3, so the indices are stable, but the elements could be nil. Or the 4th option, which will work similarly:

```swift
struct WeakObjectHolder {
    weak var object: AnyObject?
}
var elements: [WeakObjectHolder] = []

```

I wonder what is the intended use of this data structure?

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [June 30, 2023, 3:32am UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899/4 "2023-06-30T03:32:54Z")

</div>

I wanted something similar to what you’ve described, but that would reuse the space left behind by nils rather than constantly growing and having to filter out the nils manually. I don’t have a practical application yet, this was mostly just a self-imposed exercise. In part because I’ve had to reimplement common data structures in C++ for class before, I figured it would be easier to reimplement the vector from scratch instead of anticipating when a standard `Array` would try to resize.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 30, 2023, 7:32am UTC](https://forums.swift.org/t/can-a-group-of-weak-references-conform-to-collection/65899/5 "2023-06-30T07:32:00Z")

</div>

> [@tera](#):
>
> ```swift
> struct WeakObjectHolder {
> weak var object: AnyObject?
> }
> var elements: [WeakObjectHolder] = []
> 
> ```

If you are using this structure, then on insert you'd need to scan the elements, find the first entry with `nil` and replace it with the new element, if there are no nils then append a new entry.

> [@bbrk24](#):
>
> I don’t have a practical application yet

The reason I asked, if for example that was to implement a cache - that'd be the wrong approach.

I'd only do this once there's a practical need - the particular application might heavily influence the design of this data structure.
