On Saturday, 18 November 2017 at 05:24:30 UTC, Tony wrote:
Forgot to handle pre-mature foreach exit: import std.stdio : writeln; class RefRange { int foreach_index; int[] items; this(int[] src) { items = src; } bool empty() { if (foreach_index == items.length) { foreach_index = 0; // reset for another foreach return true; } return false; } int front() { return items[foreach_index]; } void popFront() { foreach_index++; } void resetIteration() { foreach_index = 0; } } void main() { int[] ints = [1, 2, 3]; auto refRange = new RefRange(ints); writeln("Ref 1st Run:"); foreach(i; refRange) { writeln(i); if ( i == 2 ) { refRange.resetIteration(); break; } } assert( ! refRange.empty); writeln("Ref 2nd Run:"); foreach(i; refRange) writeln(i); // works } ------------------------------- Ref 1st Run: 1 2 Ref 2nd Run: 1 2 3