foreach: compiler warning

Chris wendlec at tcd.ie
Tue Jul 30 07:27:16 PDT 2013


I don't know if this has been raised before. I compiled an old 
program which has a foreach loop like this:

foreach(i; 0..10) {
   if (condition) {
     i--; // deprecated
   }
}

dmd 2.063 compiled it without giving me any deprecation warning, 
but the results were screwed up big time in the program. The loop 
didn't work as expected. ldc2 gave me this hint:

Deprecation: variable modified in foreach body requires ref 
storage class

So I changed the loop to

foreach(ref i; 0..10) {
   if (condition) {
     i--;
   }
}

and it worked fine in both versions, dmd and ldc2. Mind you, the 
ldc2 compiled version of the program worked correctly with the 
deprecated code too. Only the dmd version didn't, although it 
didn't complain at compile time.

But maybe this is a well known issue, I haven't checked all 
issues yet.


More information about the digitalmars-d-ldc mailing list