[swift-users] SequenceType vs. CollectionType

Aaron Bohannon aaron678 at gmail.com
Tue Jun 14 16:05:15 CDT 2016


Hi,

I find the SequenceType protocol a bit confusing.  My confusion is best
explained in the context of a specific, real-world example.  Let's say that
I wanted to extend a protocol with a method "repeated(Int)" that would
allow me to iterate over a collection of items multiple times, like this:

for x in ["a", "b", "c"].repeated(3) {
  print(x, terminator: "")
}

That would be expected to print the string "abcabcabc".

Implementing the extension is simple enough.  Here's my main question: is
it more appropriate to implement it by extending SequenceType or extending
CollectionType?  I'm not imagining an implementation that does any
buffering, so one could argue that the operation would not have predictable
behavior in the context of an arbitrary SequenceType -- however, a likely
implementation would rely upon nothing more than the the generate() method,
which is available in the SequenceType protocol.

- Aaron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160614/1fc6fc5a/attachment.html>


More information about the swift-users mailing list