www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4282] New: Problem in AAs with fixed size arrays as keys

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4282

           Summary: Problem in AAs with fixed size arrays as keys
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: sean invisibleduck.org
        ReportedBy: bearophile_hugs eml.cc



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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4282


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |kennytm gmail.com
         Resolution|                            |FIXED




 (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] -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 24 2011