[SR-119] AnySequence --> Any*Collection promotion

Hello all,

Currently there exist at least four 'type-erased' sequence/collection types: AnySequence, AnyForwardCollection, AnyBidirectionalCollection, and AnyRandomAccessCollection.

The three Any*Collection types can be conceptually arranged into a 'ladder', in which collections can be constructed unconditionally from collection types above themselves, and conditionally from collection types below themselves. I've put together a little ASCII image of this here: (https://gist.github.com/austinzheng/829425242bef1573b668\).

Note that AnySequence doesn't currently fit into this ladder: Any*Collection instances can't easily be constructed from AnySequences. SR-119 (https://bugs.swift.org/browse/SR-119\) aims to change that. As per that ticket, I propose that we add three promotion APIs, sketched out as following:

extension AnyForwardCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyBidirectionalCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyRandomAccessCollection {
    init?<Element>(_: AnySequence< Element>)
}

Like their Any*Collection --> Any*Collection antecedents, these initializers construct a new Any*Collection out of an existing AnySequence, but only if the underlying sequence is compatible, and without copying the underlying sequence.

I'll also have to examine SE-0014 (https://github.com/apple/swift-evolution/blob/master/proposals/0014-constrained-AnySequence.md\) more closely to see if it has any effects on this proposal.

I plan on submitting a formal proposal at some point (probably by the end of the coming week), but first wish to solicit feedback, opinions, concerns, etc. Thanks for your time.

Best,
Austin

My main concern is whether you can implement it, and do so efficiently. I don’t have a particular reason to think you can’t, but it seems simple enough to check, so IMO such a proposal should come with an implementation.

-Dave

···

On Jan 2, 2016, at 9:44 PM, Austin Zheng via swift-evolution <swift-evolution@swift.org> wrote:

Hello all,

Currently there exist at least four 'type-erased' sequence/collection types: AnySequence, AnyForwardCollection, AnyBidirectionalCollection, and AnyRandomAccessCollection.

The three Any*Collection types can be conceptually arranged into a 'ladder', in which collections can be constructed unconditionally from collection types above themselves, and conditionally from collection types below themselves. I've put together a little ASCII image of this here: (https://gist.github.com/austinzheng/829425242bef1573b668\).

Note that AnySequence doesn't currently fit into this ladder: Any*Collection instances can't easily be constructed from AnySequences. SR-119 (https://bugs.swift.org/browse/SR-119\) aims to change that. As per that ticket, I propose that we add three promotion APIs, sketched out as following:

extension AnyForwardCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyBidirectionalCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyRandomAccessCollection {
    init?<Element>(_: AnySequence< Element>)
}

Like their Any*Collection --> Any*Collection antecedents, these initializers construct a new Any*Collection out of an existing AnySequence, but only if the underlying sequence is compatible, and without copying the underlying sequence.

I'll also have to examine SE-0014 (https://github.com/apple/swift-evolution/blob/master/proposals/0014-constrained-AnySequence.md\) more closely to see if it has any effects on this proposal.

I plan on submitting a formal proposal at some point (probably by the end of the coming week), but first wish to solicit feedback, opinions, concerns, etc. Thanks for your time.

Agreed, any proposal would come with an accompanying PR. Since the API itself is pretty straightforward, I don't think this will put any undue burden on the reviewers.

Austin

···

On Jan 3, 2016, at 12:09 AM, Dave Abrahams <dabrahams@apple.com> wrote:

On Jan 2, 2016, at 9:44 PM, Austin Zheng via swift-evolution <swift-evolution@swift.org> wrote:

Hello all,

Currently there exist at least four 'type-erased' sequence/collection types: AnySequence, AnyForwardCollection, AnyBidirectionalCollection, and AnyRandomAccessCollection.

The three Any*Collection types can be conceptually arranged into a 'ladder', in which collections can be constructed unconditionally from collection types above themselves, and conditionally from collection types below themselves. I've put together a little ASCII image of this here: (https://gist.github.com/austinzheng/829425242bef1573b668\).

Note that AnySequence doesn't currently fit into this ladder: Any*Collection instances can't easily be constructed from AnySequences. SR-119 (https://bugs.swift.org/browse/SR-119\) aims to change that. As per that ticket, I propose that we add three promotion APIs, sketched out as following:

extension AnyForwardCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyBidirectionalCollection {
    init?<Element>(_: AnySequence< Element>)
}

extension AnyRandomAccessCollection {
    init?<Element>(_: AnySequence< Element>)
}

Like their Any*Collection --> Any*Collection antecedents, these initializers construct a new Any*Collection out of an existing AnySequence, but only if the underlying sequence is compatible, and without copying the underlying sequence.

I'll also have to examine SE-0014 (https://github.com/apple/swift-evolution/blob/master/proposals/0014-constrained-AnySequence.md\) more closely to see if it has any effects on this proposal.

I plan on submitting a formal proposal at some point (probably by the end of the coming week), but first wish to solicit feedback, opinions, concerns, etc. Thanks for your time.

My main concern is whether you can implement it, and do so efficiently. I don’t have a particular reason to think you can’t, but it seems simple enough to check, so IMO such a proposal should come with an implementation.

-Dave