# RangeReplaceableCollection

**URL:** <https://forums.swift.org/t/rangereplaceablecollection/27839>\
**Category:** Discussion\
**Created:** [August 13, 2019, 5:57am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839 "2019-08-13T05:57:17Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [August 13, 2019, 5:57am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/1 "2019-08-13T05:57:17Z")

</div>

Has the Standard Library been modified to extend `RangeReplaceableCollection`?

The release notes for MacOS Catalina Beta 5 report:

> The [`RangeReplaceableCollection`](https://developer.apple.com/documentation/swift/rangereplaceablecollection) protocol is extended to include a [`remove(atOffsets:)`](https://developer.apple.com/documentation/swift/rangereplaceablecollection/3349502-remove) method and the [`MutableCollection`](https://developer.apple.com/documentation/swift/mutablecollection) protocol is extended to include a [`move(fromOffsets:toOffset:)`](https://developer.apple.com/documentation/swift/mutablecollection/3348325-move) method. Each new method takes [`IndexSet`](https://developer.apple.com/documentation/foundation/indexset) instances that you use with the _onMove(perform:)_ and _onDelete(perform:)_ modifiers on [`ForEach`](https://developer.apple.com/documentation/swiftui/foreach) views. (51991601)

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [August 13, 2019, 6:53am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/2 "2019-08-13T06:53:38Z")

</div>

It's in the SwiftUI section of the report, which suggests that the extension is in the SwiftUI framework and not Standard Library

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 13, 2019, 7:46am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/3 "2019-08-13T07:46:52Z")

</div>

Shouldn't we open an issue? Is it not a "sin", for a given module A, to publicly extend a type defined in another module B with methods whose signature does not involve types from A?

```swift
// In module A
import B
extension B.SomeType {
    // Well-behaved
    public func doStuff(_ x: A.Stuff) // OK

    // Mis-behaved
    public func doStuff() 
    public func doStuff(_ x: Swift.Int)
    public func doStuff(_ x: Foundation.IndexSet)
    public func doStuff(_ x: SomeOtherModule.Stuff)
}

```

To me, it looks like SwiftUI may well use this extension internally, no problem, but that it should not be public.

Am I correct? Or do I forget things like inlining and ABI stability?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 13, 2019, 8:04am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/4 "2019-08-13T08:04:16Z")

</div>

> [@gwendal.roue](#):
>
> Is it not a "sin", for a given module A, to publicly extend a type defined in another module B with methods whose signature does not involve types from A?

No. The "sin" is in _conforming_ types you don't own to protocols you don't own. Extending a protocol with new methods is not considered problematic, whether you own any of the types involved.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 13, 2019, 8:08am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/5 "2019-08-13T08:08:42Z")

</div>

You are right @Avi, you have described the classic sin.

But aren't we looking at _another_ sin, then? Consider this [other current discussion](https://forums.swift.org/t/implementing-a-collection-removingindicies/27681/23), which talks about removing indices. What will happen when the Standard Library defines the same extension, and a user imports Foundation? Or, more precisely, if Foundation defines the same extension (after all, `IndexSet` is Foundation's property)? Won't the compiler complain with ambiguity errors, due to two conflicting implementations?

Should SwiftUI be allowed to step on Foundation's "natural" territory?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 13, 2019, 8:15am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/6 "2019-08-13T08:15:45Z")

</div>

Yes, that's another situation we have with the ability to extend types we don't own. We would need to overhaul, in a very source-breaking way, the current access controls to close this hole. Any two libraries can extend a common type (from the stdlib or any other source) in incompatible ways. This isn't limited to just the type owner and a consuming framework.

Or we need a way to disambiguate at the call site.

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [August 13, 2019, 8:17am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/7 "2019-08-13T08:17:36Z")

</div>

> [@gwendal.roue](#):
>
> if Foundation defines the same extension

Both SwiftUI and Foundation are being owned by apple, so they can remove the extension from SwiftUI at the same time they add it to Foundation. I have no idea how that would affect ABI, but it should be source compatible because SwiftUI and Foundation are always shipped together

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 13, 2019, 8:19am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/8 "2019-08-13T08:19:09Z")

</div>

> [@cukr](#):
>
> because SwiftUI and Foundation are always shipped together

And on those platforms where this is not true, SwiftUI doesn't exist, so the extension doesn't either.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 13, 2019, 8:22am UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/9 "2019-08-13T08:22:34Z")

</div>

Thanks @cukr and @Avi. I don't see any trouble now.

---

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [August 13, 2019, 2:16pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/10 "2019-08-13T14:16:14Z")

</div>

The change appears to be in the Standard Library itself, rather than in SwiftUI. Or, at least, in the [Standard Library documentation](https://developer.apple.com/documentation/swift/rangereplaceablecollection).

---

<div class="post-metadata">

**Author:** ![Spencer\_Kohan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spencer_kohan/32/4505_2.png) [@Spencer\_Kohan](https://forums.swift.org/u/Spencer_Kohan)\
**Post date:** [August 13, 2019, 2:21pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/11 "2019-08-13T14:21:12Z")

</div>

> [@Avi](#):
>
> The "sin" is in _conforming_ types you don't own to protocols you don't own.

What problems does this create? I have run into several situations where it would be _extremely_ convenient to add conformances in a separate module, and it's always been a point of frustration not to be able to do this.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 13, 2019, 2:32pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/12 "2019-08-13T14:32:20Z")

</div>

You _can_ do that, so I'm not sure what trouble you ran into.

The reason it's considered a sin is that your code is brittle, as the owners of the types can't possibly consider _your_ extensions when changing _their_ code.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 13, 2019, 2:38pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/13 "2019-08-13T14:38:43Z")

</div>

See: [Retroactive Conformances vs. Swift-in-the-OS](https://forums.swift.org/t/retroactive-conformances-vs-swift-in-the-os/14393)

---

<div class="post-metadata">

**Author:** ![Spencer\_Kohan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spencer_kohan/32/4505_2.png) [@Spencer\_Kohan](https://forums.swift.org/u/Spencer_Kohan)\
**Post date:** [August 13, 2019, 2:40pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/14 "2019-08-13T14:40:06Z")

</div>

If I try to publicly extend a type with a protocol conformance I get this error:

```
'public' modifier cannot be used with extensions that declare protocol conformances

```

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 13, 2019, 2:47pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/15 "2019-08-13T14:47:29Z")

</div>

I think that's because it's superfluous. If the protocol is public, the conformance will be, and if it's not, you can't make the extension public anyway.

---

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [August 13, 2019, 4:37pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/16 "2019-08-13T16:37:26Z")

</div>

Actually, with a little more digging, it appears the `remove(atOffsets:)` method is implemented in the `Foundation` framework. So, the documentation's listing of the method raises the question: _Should the "official" Swift Standard Library documentation include references to methods contained in extensions implemented via frameworks?_ The documentation notes that the method is part of the `SwiftUI` framework, but that approach to documentation seems awkward--perhaps a bit Apple-centric.

The `applying(_:)` method is _anticipated_ to be included in the Standard Library, but the pending amendment has not yet been accepted AFIK ([Active Review (June 19 - 25 2019)](https://github.com/apple/swift-evolution/blob/master/proposals/0240-ordered-collection-diffing.md). Of course, it is a sure thing that it will be accepted. Certainly, the formality of acceptance will catch up soon. @Douglas_Gregor

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [August 13, 2019, 4:47pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/17 "2019-08-13T16:47:08Z")

</div>

Extensions to standard library protocols and types end up getting listed in the standard library documentation, by and large.

---

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [August 13, 2019, 5:09pm UTC](https://forums.swift.org/t/rangereplaceablecollection/27839/18 "2019-08-13T17:09:11Z")

</div>

I suppose it is convenient to handle the documentation in that fashion, but it sure feels like a fork in the language. One language set when working with Apple OS, and another set when elsewhere.

I love `SwiftUI` and `Combine`, and admire the incredible, herculean work that those teams presently are engaged in. But, I am concerned about the implications of their decisions on the future development of the language.

For instance, one could imagine a future in which there would exist a `UserInterface` framework, with that framework providing (i) types to specify universal UI elements, and (ii) a set of protocols to provide an interface between those universal types and any given OS, with the platform provider (or the Swift-on-Linux project) to write types that conform to the interface protocols. `SwiftUI` may be a big step down that road; or it may, through inertia and/or due to naming collisions and/or for other reasons, become an impediment to growth in that sort of direction or other directions. I'd appreciate the thoughts of the community on that topic, but perhaps that discussion should be saved for some other thread.
