Seán Labastille


View My GitHub Profile

6 March 2016



title: EmptyCollection

In a world 🌏 driven by Entropy Harnesses sometimes your collections 📁 can feel cold ❄️ and heartless 💙⃠.

If you share this feeling, you may want to let EmptyCollection into your heart and collections. EmptyCollection provides trivial conformance to Collection (a matter which has been considered previously on these pages).

This is achieved by using an integer index type, a trivial generator struct dubbed EmptyGenerator, defining startIndex == endIndex, returning a count of 0 and disallowing subscripting.

This seems rather straightforward, so where is this put to use? The file header provides a brief motivating example:

Sometimes an operation is best expressed in terms of some other, larger operation where one of the parameters is an empty collection.

For example RangeReplaceable uses EmptyCollections to write a number of removal operations in terms of replaceRange, as does Array, while Sequence uses an EmptyCollection if a prefix of length 0 is requested.

So, the next time you need a truly empty collection, consider choosing EmptyCollection for your API.