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

2020-09-29 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 29, 2020 at 09:56:41AM +, ikod via Digitalmars-d-learn wrote: > Hello, > > Sorry if I unintentionally hurt anybody in this thread. > I'll try to implement sane and correct iteration behavior for AA > without noticeable performance loss, and propose it if I succeed. No feelings

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

2020-09-29 Thread ikod via Digitalmars-d-learn
Hello, Sorry if I unintentionally hurt anybody in this thread. I'll try to implement sane and correct iteration behavior for AA without noticeable performance loss, and propose it if I succeed. Igor

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

2020-09-29 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 27 September 2020 at 13:02:04 UTC, Per Nordlöw wrote: Is it safe to remove AA-elements from an `aa` I'm iterating over via aa.byKeyValue? I'm currently doing this: foreach (ref kv; aa.byKeyValue) { if (pred(kv.key)) aa.remove(kv.key); // ok? } if

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

2020-09-28 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/28/20 1:18 PM, ikod wrote: On Monday, 28 September 2020 at 14:58:15 UTC, Steven Schveighoffer wrote: On 9/27/20 4:43 PM, Per Nordlöw wrote: 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

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

2020-09-28 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 28, 2020 at 08:04:49PM +, ikod via Digitalmars-d-learn wrote: > On Monday, 28 September 2020 at 19:18:20 UTC, H. S. Teoh wrote: [...] > > The problem with arbitrary, unrestricted modification of a container > > while iterating over it, is that it inevitably leads to > >

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

2020-09-28 Thread ikod via Digitalmars-d-learn
On Monday, 28 September 2020 at 19:18:20 UTC, H. S. Teoh wrote: On Mon, Sep 28, 2020 at 05:18:41PM +, 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

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

2020-09-28 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 28, 2020 at 05:18:41PM +, 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

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

2020-09-28 Thread ikod via Digitalmars-d-learn
On Monday, 28 September 2020 at 14:58:15 UTC, Steven Schveighoffer wrote: On 9/27/20 4:43 PM, Per Nordlöw wrote: 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

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

2020-09-28 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/27/20 4:43 PM, Per Nordlöw wrote: 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),

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

2020-09-28 Thread Per Nordlöw via Digitalmars-d-learn
On Monday, 28 September 2020 at 10:01:23 UTC, ikod wrote: Is it specific to some types? What if collection supports stable "foreach"? Yes it depends on how collection members (such as insert, find, replace, erase, etc) are implemented. I presume we need attributes on mutating collection

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

2020-09-28 Thread ikod via Digitalmars-d-learn
On Monday, 28 September 2020 at 10:10:10 UTC, Per Nordlöw wrote: On Monday, 28 September 2020 at 10:01:23 UTC, ikod wrote: Is it specific to some types? What if collection supports stable "foreach"? Yes it depends on how collection members (such as insert, find, replace, erase, etc) are

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

2020-09-28 Thread ikod via Digitalmars-d-learn
On Monday, 28 September 2020 at 09:41:02 UTC, Per Nordlöw wrote: On Monday, 28 September 2020 at 07:15:27 UTC, Imperatorn wrote: Yes, this should be a compile-time error Spec here: https://dlang.org/spec/statement.html#foreach_restrictions Is it specific to some types? What if collection

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

2020-09-28 Thread Per Nordlöw via Digitalmars-d-learn
On Monday, 28 September 2020 at 07:15:27 UTC, Imperatorn wrote: Yes, this should be a compile-time error Spec here: https://dlang.org/spec/statement.html#foreach_restrictions

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

2020-09-28 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 27 September 2020 at 20:43:19 UTC, Per Nordlöw wrote: On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote: [...] 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

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

2020-09-28 Thread ikod via Digitalmars-d-learn
On Sunday, 27 September 2020 at 14:23:11 UTC, H. S. Teoh wrote: On Sun, Sep 27, 2020 at 01:02:04PM +, Per Nordlöw via Digitalmars-d-learn wrote: Is it safe to remove AA-elements from an `aa` I'm iterating over via aa.byKeyValue? No. Modifying a container while iterating over it is, in

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

2020-09-27 Thread Per Nordlöw via Digitalmars-d-learn
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

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

2020-09-27 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Sep 27, 2020 at 01:02:04PM +, Per Nordlöw via Digitalmars-d-learn wrote: > Is it safe to remove AA-elements from an `aa` I'm iterating over via > aa.byKeyValue? No. Modifying a container while iterating over it is, in general, a bad idea (unless the container is designed to be used

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

2020-09-27 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 27 September 2020 at 13:02:04 UTC, Per Nordlöw wrote: Is it safe to remove AA-elements from an `aa` I'm iterating over via aa.byKeyValue? I'm currently doing this: foreach (ref kv; aa.byKeyValue) { if (pred(kv.key)) aa.remove(kv.key); // ok? } if

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

2020-09-27 Thread Anonymouse via Digitalmars-d-learn
On Sunday, 27 September 2020 at 13:02:04 UTC, Per Nordlöw wrote: Is it safe to remove AA-elements from an `aa` I'm iterating over via aa.byKeyValue? I'm currently doing this: foreach (ref kv; aa.byKeyValue) { if (pred(kv.key)) aa.remove(kv.key); // ok? } if

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

2020-09-27 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 27 September 2020 at 13:02:04 UTC, Per Nordlöw wrote: Is it safe to remove AA-elements from an `aa` I'm iterating over via aa.byKeyValue? I'm currently doing this: foreach (ref kv; aa.byKeyValue) { if (pred(kv.key)) aa.remove(kv.key); // ok? } if