Bug 13756 – [AA] Allow ref const index on foreach AA iteration
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-11-20T08:17:00Z
Last change time
2015-02-18T03:39:50Z
Keywords
pull
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2014-11-20T08:17:29Z
Currently iterating AA on foreach disallows taking key value by ref.
void main()
{
int[int] aa = [1:2];
foreach ( k, v; aa) {}
foreach ( k, ref v; aa) {}
foreach (ref k, v; aa) {} // NG
foreach (ref k, ref v; aa) {} // NG
}
This is intended behavior to disallow the key object mutation stored in the hidden AA slot, but it has a drawback that we cannot avoid copying key objects in each iterations.
For the efficiency, I'd propose to allow taking key object by ref const.
void main()
{
int[int] aa = [1:2];
// typeof(k) == const(int)
foreach (ref k, v; aa) {}
// Error: index must be type const(int), not int
//foreach (ref int k, v; aa) {}
// OK
foreach (ref const int k, v; aa) {}
}
Comment #1 by bearophile_hugs — 2014-11-20T10:43:44Z