Bug 6425 – Cannot foreach over AA with char as key type
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
tools
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-08-01T09:37:00Z
Last change time
2015-06-09T04:37:38Z
Assigned to
nobody
Creator
ruzicic.aleksandar
Comments
Comment #0 by ruzicic.aleksandar — 2011-08-01T09:37:36Z
This code:
--------------------------------------
string[char] aa = [
'f': "foo",
'b': "bar"
];
foreach (ch, str; aa) {
writefln("[%s]: [%s]", ch, str);
}
--------------------------------------
Compiles fine but optlink complains:
OPTLINK (R) for Win32 Release 8.00.12
Copyright (C) Digital Mars 1989-2010 All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
test.obj(test)
Error 42: Symbol Undefined _D6object28__T16AssociativeArrayTaTAyaZ16AssociativeArray7opApplyMFMDFKaKAyaZiZi
--- errorlevel 1
while this compiles and links without problem:
--------------------------------------
string[string] aa = [
"f": "foo",
"b": "bar"
];
foreach (ch, str; aa) {
writefln("[%s]: [%s]", ch, str);
}
--------------------------------------
Comment #1 by bearophile_hugs — 2011-08-01T10:05:44Z
Playing a bit with your test-case I have found this:
void main() {
foreach (c, str; ['f': "foo"])
c++;
}
Internal error: e2ir.c 4883
Comment #2 by lovelydear — 2012-04-27T07:27:19Z
On 2.059, this gives:
PS E:\DigitalMars\dmd2\samples> rdmd bug.d
bug.d(3): Error: non-constant expression ['f':"foo",'b':"bar"]
unless one defines:
enum string[char] aa = ['f': "foo",'b': "bar"];
(See issue 5279)
But with the above declaration, both description and comment 1 test cases run fine.
Test case of description
PS E:\DigitalMars\dmd2\samples> rdmd bug.d
[b]: [bar]
[f]: [foo]
So I suggest we close this issue while keeping issue 5279 open.