A (bad) idea on wrapping a collection class

The wrapping types I've read that take in a Collection and present as another collection that channels its inner collection for elements take the inner collection by copying its value. They also conform to BidirectionalCollection and/or RandomAccessCollection if the inner collection does so. They usually don't conform to either MutableCollection or RangeReplaceableCollection since, because the inner collection is copied in, changes to the outer collection's inner collection are not reflected in the original source of the inner collection.

But that doesn't apply to class types. Since the copying of the inner collection just copies over a reference/pointer, an outer collection with the collection-mutating protocols can pass them to the original inner collection. This can be tested by:

extension MyWrapperCollection: MutableCollection where Base: MutableCollection & AnyObject { /*...*/ }

It's a neat thought experiment, but I don't think it's worth adding this contingency to the design since class-based Collections should be rare. [There's no actual policy here, so the post could be considered off-topic.]

Terms of Service

Privacy Policy

Cookie Policy