I’m disillusioned enough to not tag this thread as a pitch ;-), but as the surprisingly derailed discussion in SE-0203 — Rename Sequence.elementsEqual didn’t reveal a justification for the hierarchy of collection protocols, I hope a explicit headline might help to evoke some answers.
What do the elders say?
Luckily, sequence is a well established and old concept, so wikipedia has a good article for it - and the important parts are directly in the preview:
What do we do?
Set is not identical with the concept of a set in math - but it’s clearly modeled after it, so it’s surprising that it violates its key features (
Dictionary has similar issues, but I’ll focus on
This is not only unfortunate for mathematicians, but also the reason that
Set has some methods that even the official documentation is so ashamed of that it puts them into a separate document ;-) (https://developer.apple.com/documentation/swift/set/order_dependent_operations_on_set)
What are others doing?
Java, for example, does not make
Set conform to sequence, but to (
Iterable, which, afaics, is basically our
Sequence without the problematic methods that imply a consciously applied order.
What if we do it differently?
If we would add
Iterable and removed
Sequence conformance from problematic datatypes, there wouldn’t be the need for complicated names like
Sequence.elementsEqualInIterationOrder, which is a source breaking change.
Actually, I think breakage is good, because it would make people aware of cases where they use
Set.elementsEqual, which is most likely never what they actually want.
But we would also break all code that uses
elementsEqual in the right way.
- Remove the problematic methods from
- Add a new
Sequenceprotocol with the methods that have been removed in step 1
Arrayand other “truly” ordered collections
(feasibility isn’t confirmed ;-) - but I guess it’s harder to find flaws in concept when you don’t know what exactly the concept is)
What wouldn’t break?
Changing the protocol hierarchy is a much more fundamental change than tweaking method names, but that doesn’t necessarily mean that the consequences for existing Swift code are bigger.
Sequence would imply
Iterable, so any abstractions build on top of
Sequence would still work.
Common use of
Dictionary wouldn’t be affected either, because iteration is still possible, and testing a
Set for a prefix or reversing a
Dictionary hardly makes sense anyway.
So: What are the downsides of
Iterable that outweighs the confusion the current hierarchy causes?