HACKER Q&A
📣 amichail

Why doesn't Swift have “unpredictable order” annotations to avoid bugs?


So if you have a set s say, you would need to use something like Array(s.unpredictableOrder) to convert it to an array.

It's very easy to forget when an order is unpredictable and run into strange bugs because your code depends on consistent order.


  👤 Kon-Peki Accepted Answer ✓
I think that Swift is very clear about unpredictable ordering in sets:

> Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.

further down the page:

> Swift’s Set type doesn’t have a defined ordering. To iterate over the values of a set in a specific order, use the sorted() method, which returns the set’s elements as an array sorted using the < operator.

There is even a small snippet of example code

https://docs.swift.org/swift-book/LanguageGuide/CollectionTy...