Safe to remove AA elements while iterating over it via .byKeyValue?
H. S. Teoh
hsteoh at quickfur.ath.cx
Sun Sep 27 14:23:11 UTC 2020
On Sun, Sep 27, 2020 at 01:02:04PM +0000, Per Nordlöw via Digitalmars-d-learn wrote:
> Is it safe to remove AA-elements from an `aa` I'm iterating over via
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".
You probably want to build a list of keys to remove, then remove them
after the iteration instead.
Why can't you just be a nonconformist like everyone else? -- YHL
More information about the Digitalmars-d-learn