[Issue 4279] AAs change key type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 29 21:08:09 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4279


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich at gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2010-08-29 21:07:55 PDT ---
As of 2.048, and according to my tests, DMD forces AA's to have a const type as
the key type. For example:

import std.stdio: writeln;
void main() 
{
    int[int[]] data;
    writeln(typeid(data));
}

Prints: int[const(int)[]]


And your AA literal key type gets converted to a const type as well:

import std.stdio: writeln;
void main() 
{
    writeln(typeid([cast(char[])"foo" : 1]));
}

Prints: int[const(char)[]]

What happens here (if my interpretation is right), is that foo is first an
array of immutable chars, it's casted to a mutable array of chars, and then DMD
sees it is a key of an AA literal so it converts it to an array of const chars.

So DMD is most probably doing this:

On the left of assignment:
int[char[]] data 
-> int[const(char)[]] data

On the right of assignment:
[cast(char[])const(char)[] : int] 
-> int[cast(char[])const(char)[]] 
-> int[char[]]
-> int[const(char)[]]

And the whole thing becomes:

int[const(char)[]] data = int[const(char)[]]


So if I got that right then DMD automatically changes the key type of an AA to
be const, regardless of any casts. Having to change int[] to const(int)[]
directly in the code would probably make the AA's harder to read, so maybe
that's a good thing.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list