Revamping associative arrays
Christopher Wright
dhasenan at gmail.com
Sun Oct 18 16:02:33 PDT 2009
Moritz Warning wrote:
> On Sat, 17 Oct 2009 18:58:08 +0000, BCS wrote:
>> what will this do?
>>
>> foreach(key; aa.keys)
>> if(Test(key))
>> aa.remove(key);
>
> It's undefined behavior.
> You shouldn't try to mutate the aa while iterating.
> I hope that will be fixed.
> It took me some time to find this out.
That's really annoying, and it's true of most (all?) C# base class
library collections. When coding in C#, I often find myself doing:
var remove = new HashedSet<Foo>();
foreach (var foo in foos)
if (Test(foo)) remove.Add(foo);
foos.RemoveAll(remove);
It's more allocation than necessary, but more than that, it's an
unnecessarily complicated way of interacting with the collection.
More information about the Digitalmars-d
mailing list