Search code examples
dimmutability

In D, how can I declare a hash of immutable types that is itself mutable?


I believe I was able to do this in an earlier version of the language, and indeed, code I wrote several months ago, which compiled fine then, does not compile now. Example:

immutable(X)[int] myhash;
myhash[5] = some_immutable_X; //previously fine.
myhash[5] = some_other_immutable_X; //previously fine also.

Now however, dmd complains with

Error: cannot modify immutable expression myhash[5]

I've experimented with some other possible syntax without success (e.g. (immutable X)[int]). It seems there is no longer a way to declare that the hash itself is mutable, but the contents are not? This seems like a fairly common use case: a data structure for storing references to things that ought not to be altered. Anyone have some insight into this?


Solution

  • If it ever worked, it was a bug (probably due to the use of void* and incorrect casting somewhere in the AA implementation, since it hasn't been properly switched to templates yet AFAIK). You cannot mutate immutable values, and when you do

    myHash[5] = value;
    

    and the elements in myHash are immutable, then you are attempting to mutate an immutable value, even if it's the init value for that type (since an AA element gets initialized with the init value before it's assigned to, and the type system has no way of knowing whether the element was previously in the AA, so it can't treat the first assignment via [] as initialization and the others as assignment). If you want to have an AA of immutable elements, then you're going to need another level of indirection so that the elements themselves are not immutable but rather refer to something which is immutable - e.g by using a mutable pointer to an immutable type, or if you're dealing with classes, then use std.typecons.Rebindable (since you can't have mutable class references to const or immutable objects).