[swift-evolution] [Draft] Rename Sequence.elementsEqual

Kevin Nattinger swift at nattinger.net
Mon Oct 16 13:57:56 CDT 2017

> On Oct 16, 2017, at 10:43 AM, BJ Homer via swift-evolution <swift-evolution at swift.org> wrote:
>> On Oct 16, 2017, at 8:20 AM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> Am 16.10.2017 um 07:19 schrieb Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>>:
>>> What useful generic algorithms would this protocol support that are not already possible?
>> It would allow expressing generic algorithms depending on an order.
>> -Thorsten
> We can already express generic algorithms that depend on an order—any generic algorithm that works on a Sequence works on something that is ordered. A Swift Set has an undefined order right now, but a generic algorithm working on any arbitrary Sequence likely doesn’t care about what the order, just that an order exists. And a Swift Set does indeed have an order. If you have a generic algorithm that only works on inputs sorted in a particular manner, then you’ve likely either documented that or added a “sortedBy” parameter. Otherwise, you probably just want to be able to iterate through everything.
> Let’s assume, though, that you wanted to write an algorithm that works only on MeaningfullyOrdered inputs. 
> func extractInfo<T: MeaningfullyOrdered>(_ input: T) { }
> extractInfo(someArray)
> What stops the caller from simply wrapping the Set in an Array?
> extractInfo(Array(someSet))
> The Array constructed here is going to reflect the arbitrary ordering provided by Set, but as far as the type system is concerned, the input is an Array, which is certainly meaningfully-ordered. Have we gained anything by requiring the caller to wrap the input in an array?

We force the user to acknowledge that the unspecified order is acceptable to them, as opposed to sorting. Much like the `uniquingKeysWith` argument on Dictionary.init(keysAndValues:uniquingKeysWith:). That one even has a sane default behavior used in many other languages (`{$1}`) and still doesn't offer that as a default; whereas a set's iteration order has no sane default. I think it would even be acceptable (or even desired) to have the set's Iterator be MeaningfullyOrdered, so the user could bypass the array and use `someSet.makeIterator()`, which would probably have little if any performance or memory overhead.

> We’ve made the call site a bit more awkward, and we’ve lost a bit of performance. We certainly need to be able to convert Sets in to Arrays; to eliminate that would be massively source-breaking, and it’s not clear that allowing that conversion is actively harmful, so it’s unlikely to change in Swift 5.
> So I agree with Xiaodi; I don’t see what we would gain by splitting the protocols, other than some conceptual purity.

Conceptual purity is, e.g. why protocols with associated types are such a pain in the ass to work with. Sequence<Int> makes plenty of sense and is concise and clear (and used in, e.g. Java, C#), why should I spell it Any<T: Sequence where T.Iterator.Element == Int>, or whatever it ends up being. 4 years into the language and two of the majorly touted benefits of the language (protocols and generics) play so poorly with each other that it is one of if not THE major pain point in the language. All because a couple people wanted conceptual purity.

I'm quite sure I've seen the conceptual purity argument used in favor of many other suggestions on this list, though I don't have the time to go through and find them.

> Some have expressed concern over the existence of someSet.first, but even if we removed it, it would still be available as Array(someSet).first. And we still haven't any examples of actual algorithms that would surprise the user by behaving incorrectly when given an arbitrarily-ordered sequence, so it’s hard to make the argument that this restriction is actively harmful.

equalElements(), the original motivation for this proposal, is actively harmful on unordered Sequences. On any two instances of any unordered Sequence that are `==`, it could come back true or false. And adding and removing an element to one of them (so the same instance is back in the same state), could change the result. I don't think anyone new to the language would expect that. 

> I agree that isOrderedEqual(to:) is a better name for elementsEqual()

Much more acceptable than the proposed name.

> -BJ
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

More information about the swift-evolution mailing list