foreach problem

Chris Nicholson-Sauls ibisbasenji at gmail.com
Fri Jan 5 13:35:52 PST 2007


BCS wrote:
> Frits van Bommel wrote:
>> Dejan Lekic wrote:
>>> Compiling foreach_bug.d produces an error, while compiling 
>>> foreach_ok.d does
>>> not. IMHO that is a bug - it is clearly stated in current 
>>> specification of The
>>> Language 
>>> (http://www.digitalmars.com/d/statement.html#ForeachStatement) that
>>> variable can be declared outside.
>>
>> No it's not. I just reread all of it. I didn't find any mention of 
>> that, nor a code sample that would support your case.
>> Furthermore, if it _did_ state that somewhere, it'd be a 'bug' in the 
>> specification, not in the compiler.
>>
> 
> The spec actually asserts the exact opposite (but you really have to 
> look carefully to see it).
> 
[snip]
> To avoid some bugs when doing this unintentionally, maybe require 
> something like this:
> 
> int[] a = ...;
> int e;
> 
> foreach(alias e; a) if(0 == e % 2) break;

Not a bad idea.  I can't even think of anything better than 'alias' to use for it. 
Perhaps there should be an enhancement request filed on the bugzilla?

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-bugs mailing list