[swift-dev] [Proposal] RangeReplaceableCollection should inherits from MutableCollection

Jordan Rose jordan_rose at apple.com
Tue Dec 5 16:48:38 CST 2017



> On Dec 4, 2017, at 18:48, Brent Royal-Gordon via swift-dev <swift-dev at swift.org> wrote:
> 
>> On Dec 2, 2017, at 12:31 PM, Cao, Jiannan via swift-dev <swift-dev at swift.org> wrote:
>> 
>> I'd like to discuss the relation between RangeReplaceableCollection and MutableCollection
>> 
>> MutableCollection is a Collection type that can { set } any element with subscript(position: Index). (and also { set } for subscript(range: Range<Index>))
>> 
>> RangeReplaceableCollection requires a function replaceRange(_:with:)
>> 
>> If a type conforms to RangeReplaceableCollection, it means any of its range can be replaced, that includes the situation to replace only one element.
>> So if some type conforms to RangeReplaceableCollection, it is sufficient to be a MutableCollection.
>> So I think the RangeReplaceableCollection should conforms to MutableCollection should inherits from MutableCollection.
> 
> 
> I thought this too a couple years ago, but it's not really true. `MutableCollection` requires not only that you be able to set elements, but also that when you do so, it doesn't change any of the indices in the collection. Some collections can't guarantee that. For example, `String` can't conform to `MutableCollection` because if you set some character in the middle of the string to a character that's a different size, it might move characters later in the string to different indices. So Swift lets you say `myString.replaceSubrange(myRange, with: newCharacters)`, but it doesn't let you say `myString[myRange] = newCharacters`.
> 
> `MutableCollection` and `RangeReplaceableCollection` are very similar in that they both involve changing a collection. But they are different *kinds* of changes to a collection, so it makes sense for a collection to support either one of them without supporting the other.

MutableCollection's subscript setter is also expected to take constant time (well, proportional to the size of a single element). That may not be the case for a range replacement implementation, even if it turns out the other elements in the collection don't have to move.

Jordan


More information about the swift-dev mailing list