apple / swift-algorithms

Commonly used sequence and collection algorithms for Swift
Apache License 2.0
5.9k stars 435 forks source link

Conditionally conform Chain2Sequence to MutableCollection #217

Open ffried opened 8 months ago

ffried commented 8 months ago

Is there a specific reason why Chain2Sequence does not conform to MutableCollection when Base1 and Base2 do?

I'm trying to build an API that uses two some MutableCollection<T> as parameters and one as return value. I need it to be mutable, because I need to change a specific element in the collection. Internally, the parameters will be chained using chain(_:_:) - or at least that's the goal.

I'm happy to provide a PR for this, but I wanted to see if there's a reason against it first.

natecook1000 commented 8 months ago

These kinds of wrapper collections typically aren't mutable – for example, when you called reversed() on an array, the elements of the resulting ReversedCollection aren't mutable, even if your original array is.

That said, I don't think it's necessarily the wrong thing to do here – in general the first mutation will make a copy of whichever base collection ends up getting modified, and from then it will be singly referenced. I've started a discussion in the forums to talk about the question – please add any more thoughts you have: https://forums.swift.org/t/making-collection-wrappers-mutable/69142