Foreach Access Violation

Chris R. Miller lordsauronthegreat at gmail.com
Fri Oct 24 16:46:57 PDT 2008


Steven Schveighoffer wrote:
> "Chris R. Miller" wrote
>> BCS wrote:
>>> IIRC the docs say is it illegal to alter the loop array inside of a 
>>> foreach
>> Hmm, I wonder if it would be possible to make the loop array's length 
>> constant (immutable, whatever we're using today) in the loop so as to 
>> catch the bug at compile time.  At my current state of unenlightened 
>> thinking I don't think it'd be necessarily hard to do - but I'm not 
>> in-the-know, so I'm just bouncing an idea around.
> 
> Counter-case:
> 
> int[] arr;
> 
> foo()
> {
>    arr.length = arr.length - 1;
> }
> 
> foo2()
> {
>    foo();
>    foreach(i; arr)
>    {
>        foo();
>    }
> }
> 
> How do you compile foo?  because you are not sure whether arr is going to be 
> being used in a foreach loop (I'm calling it both inside and outside).

Hmm, the idea bounced once and then hit the floor like a bacci ball.  Oh 
well, you never know unless you try to think it through...



More information about the Digitalmars-d mailing list