AA.remove in foreach && AA = new vs cleaning

Saaa empty at needmail.com
Wed Oct 21 21:45:36 PDT 2009


Steven Schveighoffer wrote:
> Yes, behavior is undefined.
>
> from http://digitalmars.com/d/2.0/statement.html#ForeachStatement :
>
> "The aggregate must be loop invariant, meaning that elements to the 
> aggregate cannot be added or removed from it in the [loop body]"
I suspect removing the current key isn't a problem in the current 
implementation;
Been using it quite a lot without any problems :)
But I've changed everything to new the array afterwards as I deleted all the 
keys anyways.
>
> I have gotten around this in dcollections by removing elements outside the 
> loop body.  See for example the keypurge function of HashMap 
> (http://www.dsource.org/projects/dcollections/docs/current/dcollections.HashMap.html)
Do you add the ones to be deleted to a dynamic array or do you loop
over all elements afterwards? I expect the first :)






More information about the Digitalmars-d-learn mailing list