-
-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AA] AAs change key type #17226
Labels
Arch:x86
Issues specific to x86
Druntime:AA
Specific to Associative Arrays
Druntime
Specific to druntime
OS:Windows
P3
Severity:normal
Comments
andrej.mitrovich (@AndrejMitrovic) commented on 2010-08-29T21:07:55ZAs 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. |
schveiguy (@schveiguy) commented on 2010-08-30T06:18:44ZI think changing the key type to const during iteration is a useless gesture, and just serves as an annoyance rather than a guarantee.
See bug 4410 that I reported later but for a different reason. |
andrej.mitrovich (@AndrejMitrovic) commented on 2010-08-30T06:47:23Z(In reply to comment #2)
> I think changing the key type to const during iteration is a useless gesture,
> and just serves as an annoyance rather than a guarantee.
>
> See bug 4410 that I reported later but for a different reason.
In this case it doesn't change the key type during iteration, it changes it in the declaration:
import std.stdio: writeln;
void main()
{
int[char[]] data = [cast(char[])"foo" : 1];
writeln(typeid(data));
}
Prints: int[const(char)[]]
Unless I got it wrong here. :) |
schveiguy (@schveiguy) commented on 2010-08-30T06:57:47ZOh, I didn't notice that.
I was looking at bearophile's code.
But my point is the same -- converting to const does not guarantee anything. In reality, the only place where you are affected is during iteration, as everywhere else, the key is an input. |
hsteoh commented on 2012-02-27T17:34:23Zconst handling in AA's is badly broken; see bug 7512. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Arch:x86
Issues specific to x86
Druntime:AA
Specific to Associative Arrays
Druntime
Specific to druntime
OS:Windows
P3
Severity:normal
bearophile_hugs reported this on 2010-06-06T04:54:52Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=4279
CC List
Description
The text was updated successfully, but these errors were encountered: