Safe to remove AA elements while iterating over it via .byKeyValue?

Per Nordlöw per.nordlow at gmail.com
Sun Sep 27 20:43:19 UTC 2020


On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote:
> No.  Modifying a container while iterating over it is, in 
> general, a bad idea (unless the container is designed to be 
> used that way, but even then, such removal is generally 
> restricted), because it often leads to highly counterintuitive 
> results.  In the case of AA's, removing an element may lead to 
> a rehashing, which reorders elements, and your iteration may 
> miss some elements or repeat some elements or terminate 
> prematurely. Even without a rehashing, you may encounter 
> inconsistent behaviours, like some elements going "missing".

I believe it's high time we start thinking about detecting these 
violations at compile-time. I recall it's in the spec somewhere 
so we should start a deprecation process at least for AAs.


More information about the Digitalmars-d-learn mailing list