range methods on associative arrays

Paul Backus snarwin at gmail.com
Sat Jul 2 23:53:15 UTC 2022


On Saturday, 2 July 2022 at 23:13:43 UTC, Steven Schveighoffer 
wrote:
> Also, an AA is fundamentally different. I can popFront on a 
> slice, and it doesn't affect other slices. If I popFront an 
> element from an AA, it affects all other references to that AA.

To be fair this is also true of some ranges.

The real usability issue here is that if AAs a ranges with 
reference semantics, then code like this

```d
auto aa = ["foo": 123, "bar": 456];
foreach (key, val; aa)
     doSomethingWith(key, val);
```

...will consume the range, leaving `aa` empty after the loop 
completes. So most of the time, you will want to use 
`.byKeyValue` anyway, even if AAs implement the range interface 
"natively."


More information about the Digitalmars-d mailing list