On Mon, Oct 16, 2017 at 14:55 Kevin Nattinger via swift-evolution < swift-evolution@swift.org> wrote:
> On Oct 16, 2017, at 11:23 AM, David Sweeris via swift-evolution < > swift-evolution@swift.org> wrote: > > > On Oct 16, 2017, at 10:42, BJ Homer via swift-evolution < > swift-evolution@swift.org> wrote: > > On Oct 16, 2017, at 8:20 AM, Thorsten Seitz via swift-evolution < > swift-evolution@swift.org> wrote: > > > Am 16.10.2017 um 07:19 schrieb Xiaodi Wu <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. > > > Should/could we just rename `Set` to `UniquedArray` or something like > that? This is starting to feel a bit like the access control debate. > > > So essentially convert Set to OrderedSet and not offer a theoretical > unordered Set? I think that would be acceptable (if we apply it to > dictionaries as well), BUT that doesn't address the more general case of > other, potentially custom unordered Sequences. > Think of it this way: all Swift stdlib types that model unordered sequences are in fact ordered—no attempt is made to support the modeling of unordered sequences as unordered Swift types, custom or built-in. > > - Dave Sweeris > _______________________________________________ > swift-evolution mailing list > swift-evolution@swift.org > https://lists.swift.org/mailman/listinfo/swift-evolution > > _______________________________________________ > swift-evolution mailing list > swift-evolution@swift.org > https://lists.swift.org/mailman/listinfo/swift-evolution >
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution