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
This seems a nice idea.
Comment #2 by k.hara.pg — 2014-11-20T12:00:21Z
Comment #3 by github-bugzilla — 2014-11-20T16:34:16Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7a0d2e5861d4896ca689658c4077d50e8665095f fix Issue 13756 - [AA] Allow ref const index on foreach AA iteration https://github.com/D-Programming-Language/dmd/commit/e1e13686757d4d36b3ff39111a805c730fab17f3 Merge pull request #4152 from 9rnsr/fix13756 Issue 13756 - [AA] Allow ref const index on foreach AA iteration
Comment #4 by github-bugzilla — 2015-02-18T03:39:50Z