> Am 16.10.2017 um 19:42 schrieb BJ Homer <bjho...@gmail.com>:
>
>> On Oct 16, 2017, at 8:20 AM, Thorsten Seitz via swift-evolution
>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>
>>> Am 16.10.2017 um 07:19 schrieb Xiaodi Wu <xiaodi...@gmail.com
>>> <mailto:xiaodi...@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.
>
> So I agree with Xiaodi; I don’t see what we would gain by splitting the
> protocols, other than some conceptual purity. 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.
>
> I agree that isOrderedEqual(to:) is a better name for elementsEqual()
IMHO `elementsEqual` provides a nice example for a method which only makes
sense on something meaningfully ordered:
What is the use case for `elementsEqual` that works with a Set?
-Thorsten
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution