Foreach Access Violation

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 21 06:18:39 PDT 2008


"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).

-Steve 





More information about the Digitalmars-d mailing list