Is removing elements of AA in foreach loop safe?

berni someone at somewhere.com
Fri Aug 30 16:45:20 UTC 2019


On Friday, 30 August 2019 at 15:00:59 UTC, Paul Backus wrote:
> Whether you actually get an error at runtime depends on the 
> load factor of the AA. If it drops below a certain threshold, 
> the AA will be resized [1], and its original memory will be 
> freed [2].

It could still work, depending on how the foreach loop is 
implemented. If the keys were stored away before starting the 
loop it would work. But for one thing, it isn't implemented that 
way and for the other, one shouldn't rely on it, because the 
implementation could change. What I hoped for, was, that the 
specs enforce somewhere, that this is to be implemented in a safe 
manner.

I'll replace this loops by something better, e.g. the mentioned 
filter. But I've never worked with AAs and filters yet. Will see, 
if I manage to do that. Else I'll probably just copy the keys and 
use them for an independent loop.



More information about the Digitalmars-d-learn mailing list