I am trying to write my own version IndexingIteratorto increase understanding Sequence. I have not assigned any type to map Iterator in my structure. However, the compiler does not complain about this, and I get a standard implementation makeIterator.
Below are my codes:
struct __IndexingIterator<Elements: IndexableBase>: Sequence, IteratorProtocol {
mutating func next() -> Elements._Element? {
return nil
}
}
let iterator = __IndexingIterator<[String]>()
iterator.makeIterator()
I think there Sequenceshould be some extensions that add a default implementation. So I searched it in Sequence.swiftand just found it.
extension Sequence where Self.Iterator == Self, Self : IteratorProtocol {
public func makeIterator() -> Self {
return self
}
}
I thought it would be like this:
extension Sequence where Self: IteratorProtocol {
typealias Iterator = Self
...
}
Am I missing something or misunderstood the extension?
source
share