[swift-evolution] [Draft] Rename Sequence.elementsEqual
David Sweeris
davesweeris at mac.com
Mon Oct 16 13:22:45 CDT 2017
> On Oct 16, 2017, at 10:42, 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> wrote:
>>>
>>> Am 16.10.2017 um 07:19 schrieb Xiaodi Wu <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’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.
Should/could we just rename `Set` to `UniquedArray` or something like that? This is starting to feel a bit like the access control debate.
- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171016/e52e1339/attachment.html>
More information about the swift-evolution
mailing list