About foreach loops

Daniel Gibson metalcaedes at gmail.com
Wed Jun 15 07:36:15 PDT 2011


Am 15.06.2011 16:37, schrieb Kagamin:
> 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.

maybe with foreach(ref i;0..10) ?

> 
>> 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