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

Austin Zheng austinzheng at gmail.com
Sat Jan 2 23:44:41 CST 2016


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 <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 <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 <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160102/1b5ed5c7/attachment.html>


More information about the swift-evolution mailing list