[swift-evolution] Proposal: CollectionType.cycle property for an infinite sequence

Kevin Ballard kevin at sb.org
Wed Dec 30 17:18:35 CST 2015


On Wed, Dec 30, 2015, at 02:27 PM, plx via swift-evolution wrote:
>
>> On that note, core language team, anyone know offhand why
>> SequenceType has a bunch of extension methods that aren't part of the
>> protocol? For example, contains(_ predicate:). The only real reason I
>> can think of is to shrink the protocol witness table, but surely
>> that's not particularly meaningful. I warrant that contains(_
>> predicate:) doesn't really have any reason to be overridden by
>> anything except sequences that knowingly repeat elements (e.g.
>> CycleSequence and Repeat), and even that's only if you assume the
>> predicate is pure, but there's some other methods that make sense to
>> override on some sequences (like minElement(_ isOrderedBefore:) for
>> any sequence that has a defined ordering).
>
> I am not sure how, precisely, you would propose to override the closure-
> taking variant of `minElement` to take advantage of an intrinsic
> ordering, but I’d be curious to see it.

Oops, you're entirely right. I wasn't thinking carefully about it; it's
the Comparable version that would be able to take advantage of an
intrinsic ordering, but of course we can't override that using the
current language rules as it requires the Equatable bound. Of course,
once we add the ability to have conditional protocol conformance, then
we can move things like that into a separate protocol and declare
SequenceType as conforming to it whenever Element : Equatable, which
would let those methods then become overridable.

Although interestingly, contains() actually has a hack right now where
it invokes a hidden SequenceType method called
_customContainsEquatableElement(). This is explicitly intended to let
sequences override the Equatable version of contains() if the sequence
knows it can do better than a linear search.

> I’d hope that the non-closure-accepting variants would be made
> overridable once the type system supports it.

Agreed.

-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151230/c7eb555c/attachment.html>


More information about the swift-evolution mailing list