<div dir="ltr">Currently, the construct:<br><br>for x in something {}<div><br>expects &quot;something&quot; to be a SequenceType. From that variable, the underlying code retrieves a generator by calling something.generate(). The resulting GeneratorType instance is unavailable to calling code.<br><br>Unless there is a reason for the language to require a SequenceType, it seems like there are good use cases for accepting a caller-provided GeneratorType, too.<br><br>For example, it would allow continuable iterations, or more generally, the results of the loop contruct to maintain state.<br><br>struct ExampleGenerator: GeneratorType {</div><div>   typealias Element = Int</div><div><br></div><div>   var current: Int</div><div>   private var initial: Int</div><div><br></div><div>   mutating func next() -&gt; Element? {</div><div>      guard self.current &lt;= self.initial + 10 else {</div><div>        return nil</div><div>      }</div><div><br></div><div>      self.current += 1</div><div><br></div><div>      return self.current</div><div>    }</div><div>}</div><div><br></div><div>struct ExampleSequence: SequenceType {</div><div>    let start: Int</div><div><br></div><div>    func generate() -&gt; ExampleGenerator {</div><div>      return ExampleGenerator(start: self.start)</div><div>    }</div><div>}<br><br>With the current mechanism:<br><br>var seq = ExampleSequence(start: 5)<br>for x in seq {</div><div>  if (x &gt; 7) {</div><div>    break</div><div>  }</div><div>  print(x)</div><div>}<br><br>result:</div><div>5</div><div>6</div><div>7</div><div><br>for y in seq {</div><div>  print(y)<br>}</div><div><br>result:<br>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>... etc</div><div><br><br>If we could pass a Generator:<br><br>var gen = ExampleGenerator(start: 5)<br><br>for x in gen {</div><div>  if(x &gt; 7) {</div><div>    break</div><div>  }</div><div>  print(x)</div><div>}<br><br></div><div>result:</div><div>5</div><div>6</div><div>7</div><div><br></div><div>for y in gen {</div><div>  print(y)</div><div>}</div><div><br></div><div>result:<br>8</div><div>9</div><div>10</div><div>... etc</div><div><br><br>Given that the provided generator also is a persistent data structure, it could easily be used as the sink for the results of one or multiple for/in loops, such as to aggregate statistics about the iterated items, count the number of iterations, etc.<br><br>Are there downsides in the implementation or design that make this a bad idea?<br><br>Mike</div></div>