[swift-evolution] Should all GeneratorTypes also be SequenceTypes?

Haravikk swift-evolution at haravikk.me
Mon Mar 14 18:42:57 CDT 2016

There are quite a lot of generator implementations that also implement SequenceType (since it’s as simple as returning self). Indeed, the AnyGenerator type conforms to SequenceType, allowing any generator to be wrapped as a sequence, though I imagine this comes with some overhead. So it got me thinking, why doesn’t GeneratorType just require conformance to SequenceType? It would be extremely simple to provide the default implementation (return self like I say), and allow all generators to be passed seamlessly into functions that take sequences, without having to wrap them first. It also gives access to the same rich set of methods to generators directly.

I can’t really think of any reason not to do this, which is why I wanted to ask first, as I thought perhaps there’s something I’m missing. The only things I can think of are destructive sequences or generators implemented by classes (both of which could result in different references getting different elements, or one being consumed before it is used), however this is something that already requires clear documentation, since I could fall into the same trap using AnyGenerator anyway.

More information about the swift-evolution mailing list