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

Dave Abrahams dabrahams at apple.com
Fri Jul 1 11:52:20 CDT 2016


on Fri Jul 01 2016, Matthew Johnson <matthew-AT-anandabits.com> wrote:

>> On Jun 30, 2016, at 5:39 PM, Dave Abrahams <dabrahams at apple.com> wrote:
>> 
>> 
>> on Thu Jun 30 2016, Haravikk <swift-evolution-AT-haravikk.me> wrote:
>> 
>
>>>> On 30 Jun 2016, at 18:26, Dave Abrahams <dabrahams at apple.com> wrote:
>>>> 
>>>> Q: Why should there be indices on an infinite multipass sequence?  
>>>> A: Because the operations on indices apply equally well whether the
>>>>  sequence is finite or not.  Find the index of a value in the
>>>>  sequence, slice the sequence, find again, etc.
>>> 
>>> Would it not make more sense for sequences that can benefit from
>>> subscripts to just conform to Indexable? 
>> 
>> All multi-pass sequences can benefit from subscripts.
>> 
>>> It seems like having basically all of the Collection-specific methods
>>> on Sequence too would be confusing, and dilute any nothing that these
>>> are sequences where elements are supposed to be consumed in-order, as
>>> it would give the illusion that you can skip around with the
>>> convenience of an array.
>> 
>> If traversal consumes the sequence, it is a single-pass thing.
>
> It’s also worth noting that Collection does not imply the ability to
> skip around “with the convenience of an array” if that means skipping
> around in constant time.  You need RandomAccessCollection to get that
> guarantee.
>
>> 
>>> There's also the issue of how you would even index something that's
>>> potentially infinite; you'd need to use a big integer in which case
>>> you could end up with your indices growing to infinite size over time?
>> 
>> It's trivial; the index contains the iteration state.  The only
>> fundamental difference between the constraints on Iterator and the
>> constraints on an Index is that Iterator doesn't support comparison for
>> equality.
>
> Not just equality, but also general `Comparable` (you’re not
> considering changing that are you?).

As noted elsewhere: yes, I am strongly considering that.

-- 
Dave


More information about the swift-evolution mailing list