Is a set a sequence?

1

As you say, all these methods we are talking about don't break or produce harmful code on unordered sequences. Conversely, they run as usual and produce results that can vary across calls.
Just as a subscript behaves on Set. In other words, they have the right to exist and they naturally work on every sequence. They simply aren't useful for us in certain cases.

This is an example of misusing Set. If someone does misuse Set in a similar manner, it is safe to assume the person hasn't bothered reading the doc and doesn't know what he's dealing with. In particular, that Set has the semantics of an unordered collection. Is it the Standard Library's fault for providing a working method that isn't both useful or compulsory, or the consumer's fault for not reading what Set is?


2

The Insufficiently detailed (to get rid of these cases) taxonomy of the Sequence hierarchy requires a solution that implies a large breaking change to the whole taxon.

@xwu If you may,

That said, it is clear this issue is out of priority for at least Swift 5. @Ben_Cohen says it doesn't matter that some methods aren't useful – and he is right as long as their quantity is constant compared to the number of other API's, as it is at present. However, in the near future, considering recent proposals, this can and likely will change – hence the topic of complementing the Sequence clade might become relevant.

Let's discuss the changes needed.
If we add an Iterable protocol, we can't conform Set and Dictionary directly to it, since they are both Collections (no doubt about that). Sequence is now strictly ordered. But Collections can be either ordered or unordered. We will need an OrderedCollection protocol, make it inherit Collection and Sequence, while Collection with it's hierarchy moves on to Iterable. This is neither easy, neither convenient nor worth the gains, at least now. Let's see what happens when we have ~50 useless methods on Set.

Just a quick illustration:

Before

After

Not too much of a mess, yet. But how about further granulation? Infinite/finite, emphasized single-pass/multi-pass – what will this turn into? Not even close to a trivial design question.

4 Likes