# \[Pitch\] Remove the single-pass requirement on Sequence

**URL:** <https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964>\
**Category:** Pitches\
**Created:** [January 20, 2018, 6:52pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964 "2018-01-20T18:52:50Z")\
**Posts on this page:** 7\
**Page:** 4

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [January 24, 2018, 11:25pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/63 "2018-01-24T23:25:50Z")

</div>

It just occured to me after posting that there should be no way for Sequence to provide default implementations for non-mutating functions with only the mutating `next` function.

It seems the default implementations make a copy of `self`, making such a sequence multi-pass.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [January 24, 2018, 11:26pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/64 "2018-01-24T23:26:11Z")

</div>

big mood

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 24, 2018, 11:28pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/65 "2018-01-24T23:28:38Z")

</div>

> [@MutatingFunk](#):
>
> It seems the default implementations make a copy of self, making such a sequence multi-pass.

...or a reference type.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [January 24, 2018, 11:33pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/66 "2018-01-24T23:33:14Z")

</div>

You may be misunderstanding me. In the following code, it seems `map` is copying self:

> **code**
>
> ```
> struct NumberSequence: IteratorProtocol, Sequence {
> var nextItem = 0
> mutating func next() -> Int? {
> if nextItem == 10 {return nil}
> defer {nextItem += 1}
> return nextItem
> }
> }
> 
> let s = NumberSequence()
> let a = s.map{$0}
> let b = s.map{$0} //b == a when NumberSequence is a struct
> 
> ```

That aside, I agree that any single-pass sequence must have reference-sematics, to render this copying ineffective.

edit: …and all my reasoning on this topic so far was based on Iterators being single-pass when conformed to Sequence. So I'm back to square one, not understanding why Iterator and Sequence are separate protocols.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [January 24, 2018, 11:41pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/67 "2018-01-24T23:41:50Z")

</div>

> [@xwu](#):
>
> Isn’t this true by construction? Can there exist any Sequence type that’s single-pass and also has value semantics?

It occurs to me that perhaps this is a lack-of-`moveonly` issue. If `Sequence.makeIterator()` on a move-only type consumed `self`, then the type system would prevent you from treating a single-pass sequence as multi-pass, because any attempt to access the sequence's elements would destroy it.

Have we given any thought to how `Sequence` should look in a world with `moveonly` types? Have we thought about forward compatibility with such a future design?

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [January 26, 2018, 8:28pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/68 "2018-01-26T20:28:59Z")

</div>

> [@beccadax](#):
>
> Have we given any thought to how Sequence should look in a world with moveonly types? Have we thought about forward compatibility with such a future design?

Yep, they're intimately related. I think I had it all worked out at one point exactly how they should be related, but if so, I've since forgotten.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 27, 2018, 4:14am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/69 "2018-01-27T04:14:59Z")

</div>

> [@dabrahams](#):
>
> I think I had it all worked out at one point exactly how they should be related, but if so, I’ve since forgotten.

Too large to fit in the margin?

[Previous page](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964.md?page=3)
