[swift-evolution] [Pitch] Remove destructive consumption from Sequence

Haravikk swift-evolution at haravikk.me
Mon Jun 27 14:12:05 CDT 2016


> On 27 Jun 2016, at 04:56, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Can’t a Sequence be potentially infinite, whereas a collection has a defined count/endIndex?  Other than that, I agree with your statement.
> 
> Here is what I see as the appropriate structure:
> 
> Iterator: Single destructive pass, potentially infinite, (should be for-in able)
> Sequence: Guaranteed non-destructive multi-pass (vends Iterators), potentially infinite, (should be subscript-able, gain most of collection, but lose anything that relies on it ending)
> Collection: Multi-pass, guaranteed finite, (no changes from current form, except extra inits from Iterator/Sequence with end conditions)
> 
> Right now we are allowed to have an infinite sequence, but calling dropLast or non-lazy map will cause an infinite loop.  These cases could be made much safer by considering the potentially infinite and finite cases separately...

This seems pretty reasonable to me, though I'm not sure about subscripting Sequence, I mean you can always add that yourself to sequence types where it makes sense to them that way, but otherwise I think it's best to just keep it simple and leave Sequences as a type that vends iterators over the same values.

Some methods of sequence may need to be moved to iterators though, but in mutating form; things like .dropFirst() to skip elements, .first(where:) for skipping to a matching element and so-on. Iterator should probably also have the .underestimatedCount property so they can give a useful value if it can be known.

But yeah, I think the basic structure of this makes sense, as the potentially destructive nature of sequences doesn't seem that well known in my experience, and I often have to go back and check my code to be sure I've avoid possible destructive usage; I've never thought of trying to use iterators instead, might try changing some methods and see how that goes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160627/fe29cc48/attachment.html>


More information about the swift-evolution mailing list