[swift-evolution] [Review] SE-0173: Add `MutableCollection.swap(_:with:)

Brent Royal-Gordon brent at architechies.com
Wed Apr 26 04:03:51 CDT 2017

> On Apr 25, 2017, at 11:32 AM, Ted Kremenek via swift-evolution <swift-evolution at swift.org> wrote:
> What is your evaluation of the proposal?
I agree that this method should be added, but I would suggest naming it differently.

Collection parameters have to be very carefully labeled because there are so many different associated types and they are often generic. For instance, an `Array<Int>` will have the same type for `Element`, `Index`, and `IndexDistance`. In the case of `swap`, one might misunderstand the call and believe the parameters are elements or counts, rather than indices.

The simplest solution would be to change it to:

	elements.swap(at: lo, with: hi)

However, you can also make an argument for:

	elements.swapAt(lo, hi)

This style is relatively rare, but is endorsed by the API Guidelines when the preposition applies to all of the arguments collectively.

Actually, we might want to consider extending the semantics of this method to:

	mutating func swapAt(_ indices: Index...)

The method would move `indices[0]` to `indices[1]`, `indices[1]`, to `indices[2]`, etc., finally moving `indices.last!` to `indices[0]`. This would do the same thing as the proposed method when there were two indices, but would provide other useful behaviors when there were more than two.
> Is the problem being addressed significant enough to warrant a change to Swift?
Yes. Tightening the rules around simultaneous access is necessary, and this is a good way to do that.
> Does this proposal fit well with the feel and direction of Swift?
> If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
I actually prefer Foundation's use of `exchange` to `swap`, which feels less jargon-y to me, but `swap` has precedent in the standard library.
> How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Just a quick reading.

Brent Royal-Gordon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170426/b68ad0a1/attachment.html>

More information about the swift-evolution mailing list