Shared/const/immutable does not propagate to hash keys

Jonathan M Davis jmdavisProg at gmx.com
Tue Jun 14 15:13:39 PDT 2011


On 2011-06-14 13:11, Andrej Mitrovic wrote:
> shared int[int] AA;
> shared int[] arr;
> 
> void main()
> {
> arr = AA.keys;
> }
> 
> Error: cannot implicitly convert expression (AA.keys()) of type int[]
> to shared(int[])
> 
> Workaround:
> 
> shared int[shared(int)] AA;
> shared int[] arr;
> 
> void main()
> {
> arr = AA.keys;
> }
> 
> Shouldn't shared propagate to the entire type?
> The same thing seems to happen with const and immutable:
> 
> const int[const(int)] CONST = [1:1];
> auto arr1 = CONST.keys;
> writeln(typeid(arr1)); // const(int)[]
> 
> const int[int] NOTCONST = [1:1];
> auto arr2 = NOTCONST.keys;
> writeln(typeid(arr2)); // int[]
> 
> But .values doesn't suffer from the issue:
> 
> import std.stdio;
> shared (int[int]) AA;
> 
> void main()
> {
> auto arr1 = AA.keys;
> auto arr2 = AA.values;
> writeln(typeid(arr1)); // writes int[]
> writeln(typeid(arr2)); // writes shared(int)[]
> }

The thing is that the keys for an AA _must_ be immutable, even if you don't 
mark it as such. So, const means nothing for them. And I don't think that 
shared does either, because immutable variables are implicitly shared. So, I'm 
not sure that what you're trying to do even makes sense. As far as the keys 
and values properties go, I believe that they're generated from the AA and are 
copies of the values - not the originals (though obviously in the case of 
references or pointers, the stuff which is referred to or pointed to isn't 
copied - just the reference or pointer). So, why would the result be shared? 
It might work with the values but certainly not with the keys which have to be 
immutable.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list