<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><br>Yes. &nbsp;‘<span style="background-color: rgba(255, 255, 255, 0);">replaceSubrange(myRange, with: newCharacters)’ is different than subscript, because it may change the length.&nbsp;</span><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">But any subscript could do that replaceSubrange could do, right?</span><div><br>在 2017年12月5日,10:48,Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com">brent@architechies.com</a>&gt; 写道:<br><br></div><blockquote type="cite"><div><blockquote type="cite"><span>On Dec 2, 2017, at 12:31 PM, Cao, Jiannan via swift-dev &lt;<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>&gt; wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I'd like to discuss the relation between RangeReplaceableCollection and MutableCollection</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>MutableCollection is a Collection type that can { set } any element with subscript(position: Index). (and also { set } for subscript(range: Range&lt;Index&gt;))</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>RangeReplaceableCollection requires a function replaceRange(_:with:)</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>If a type conforms to RangeReplaceableCollection, it means any of its range can be replaced, that includes the situation to replace only one element.</span><br></blockquote><blockquote type="cite"><span>So if some type conforms to RangeReplaceableCollection, it is sufficient to be a MutableCollection.</span><br></blockquote><blockquote type="cite"><span>So I think the RangeReplaceableCollection should conforms to MutableCollection should inherits from MutableCollection.</span><br></blockquote><span></span><br><span></span><br><span>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 `</span><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">myString.replaceSubrange(myRange, with: newCharacters)</span></font>`, but it doesn't let you say `myString[myRange] = newCharacters`.</div></blockquote><blockquote type="cite"><div><span></span><br><span>`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.</span><br><span></span><br><span>-- </span><br><span>Brent Royal-Gordon</span><br><span>Architechies</span><br></div></blockquote></div></body></html>