On Monday, 28 September 2020 at 19:18:20 UTC, H. S. Teoh wrote:
On Mon, Sep 28, 2020 at 05:18:41PM +0000, ikod via Digitalmars-d-learn wrote:
On Monday, 28 September 2020 at 14:58:15 UTC, Steven Schveighoffer wrote:
[...]
> One could write a specific function to iterate and remove. I

This looks like dead end to me, as you may not only remove items from arbitrary positions while iterating over collection, but also insert items. Also this can happens not only inside foreach body, but in other kinds of iteration. And also there can be nested iterations over same collection.

The problem with arbitrary, unrestricted modification of a container while iterating over it, is that it inevitably leads to counterintuitive results.

Thanks for your reply, and sorry if I was not clear, but I meant AA. AA is unordered container, so for me intuitive behavior for mutations visibility during iteration is next:

1) you must not see removed keys. Let our keys are (unordered) A D C E F, you stay on D and remove E. Then E must not be seen on any future iteration steps.

2) you may or may not see any inserted keys. As AA is unordered container there is no reason to expect anything about position of key you just inserted - it can fall before or after current iteration position, so implementation is free to show inserted keys or not. I prefer not to see new keys.

3) you expect to visit all not removed keys exactly once.

Is it sane?


For example, suppose you have a container containing the elements A, B, C, D, E, and you're iterating over it in that order.

1) Suppose you're at element C, and you decide that you need to insert F. Then there's the question: should F be included in
...

2) Suppose you're at element C, and you decide to delete D. "Obviously", the current iteration should not include D. Or
...

Sure, everything you said is correct for ordered collection.


Also, what if the act of deleting D requires an internal reorganization of the container? If this reorg changes the iteration order, how should the current iteration proceed?

Intuitive behavior should follow mentioned three rules regardless of internal implementation.


Reply via email to