About foreach loops

Kagamin spam at here.lot
Wed Jun 15 07:37:55 PDT 2011


bearophile Wrote:

> In my opinion this is a bit bug-prone because in real code there is some risk of modifying the iteration variable "i" by mistake. (Note: here I am not talking about D for() loops. They are OK, their semantics is transparent enough. foreach() loops are less transparent and they *look* higher level than for() loops). I'd like the iteration variable to act as being a copy of the true loop variable as in Python.

What if user wants foreach(i;0..10) to be a shortcut for for(i=0;i<10;i++) and still modify the variable with the exception that bounds are evaluated only once? :)
Well, yeah, this makes little sense.

> If this is a bit bad for foreach performance, then I'd like the compiler to forbid the mutation of the foreach iteration variable inside the foreach body. Is this even possible?

is iota a solution here?


More information about the Digitalmars-d mailing list