Why doesn't foreach support iterating?

Chris Katko ckatko at gmail.com
Wed Oct 17 01:17:40 UTC 2018


int [50]data;
foreach(i, datum; data){} // works

File file("gasdgasd");
foreach(i, line; file.byLine){} //NOPE.
foreach(line; file.byLine){} //works.

I finally noticed in the docs it says "for arrays only." The 
question is, why?

Every language that I used previously (as far as I can remember) 
implemented foreach in a way that's consistent / orthogonal.

Is there a way around this? Because currently I have to liter my 
foreach's with ugly manual index variables that hold scope even 
after it's gone.

int i = 0;
foreach(line; file.byLine){
//do stuff
i++;
}
i = 2; // still exists!


More information about the Digitalmars-d-learn mailing list