[Issue 13756] New: [AA] Allow ref const index on foreach AA iteration
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Nov 20 00:17:29 PST 2014
https://issues.dlang.org/show_bug.cgi?id=13756
Issue ID: 13756
Summary: [AA] Allow ref const index on foreach AA iteration
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: DMD
Assignee: nobody at puremagic.com
Reporter: k.hara.pg at gmail.com
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) {}
}
--
More information about the Digitalmars-d-bugs
mailing list