range methods on associative arrays
Steven Schveighoffer
schveiguy at gmail.com
Sun Jul 3 00:25:23 UTC 2022
On 7/2/22 7:53 PM, Paul Backus wrote:
> 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.
Right, but by default iterating an AA doesn't do this, and nobody would
expect it.
>
> 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."
Technically, this doesn't use the range API.
However, aa.map!(...) would, and that's where the real problems would begin.
The proposal is somewhat cleverer in that it will implicitly call
`byKeyValue`, but this I don't think scales well, unless the compiler is
made to do it.
I think saving the call to `.byKeyValue` isn't worth this trouble.
-Steve
More information about the Digitalmars-d
mailing list