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?
An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() or cmp() method). Hashable objects which compare equal must have the same hash value.
Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can't be changed after it is created. Mutable Objects : These are of type list, dict, set . Custom classes are generally mutable.
An object is mutable if it is not immutable. An object is immutable if it consists, recursively, of only immutable-typed sub-objects. Thus, a tuple of lists is mutable; you cannot replace the elements of the tuple, but you can modify them through the list interface, changing the overall data.
Some of the mutable data types in Python are list, dictionary, set and user-defined classes. On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and range.
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).
This behaviour did not work before 2.061, it has been work from 2.061 to 2.066.1. And it is "fix" in 2.067.
More info: github pull and bug issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With