Bug 4282 – Problem in AAs with fixed size arrays as keys
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-06-06T05:37:00Z
Last change time
2015-06-09T05:13:45Z
Assigned to
sean
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-06-06T05:37:13Z
This D2 program, compiles and run with DMD v2.046:
import std.stdio: writeln;
void main() {
char[] txt = cast(char[])("this is just a test".dup);
enum int N = 2;
int[char[N]] aa;
foreach (i; 0 .. txt.length + 1 - N) {
char[2] key = txt[i .. i + N];
aa[key]++;
}
writeln(aa);
}
The correct output:
[[t,h]:1,[i,s]:2,[u,s]:1,[t, ]:1,[ ,t]:1,[e,s]:1,[h,i]:1,[ ,i]:1,[ ,a]:1,[a, ]:1,[t,e]:1,[ ,j]:1,[s, ]:2,[j,u]:1,[s,t]:2]
-----------------
But after this small change:
import std.stdio: writeln;
void main() {
char[] txt = cast(char[])("this is just a test".dup);
enum int N = 2;
int[char[N]] aa;
foreach (i; 0 .. txt.length + 1 - N) {
aa[txt[i .. i + N]]++;
}
writeln(aa);
}
It doesn't work, inside aa goes only one key-value pair.
(Similar traps are very bad in a language. In this situation I suggest to produce a compile-time error, or better to convert the slice into the correct 2 char array to be used as key).
Comment #1 by kennytm — 2011-06-24T13:22:46Z
(In reply to comment #0)
> (Similar traps are very bad in a language. In this situation I suggest to
> produce a compile-time error, or better to convert the slice into the correct 2
> char array to be used as key).
DMD now errors with
Error: cannot implicitly convert expression (txt[i..i + 2u]) of type char[] to char[2u]