Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

immutability

d

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?

like image 612
John Doucette Avatar asked Apr 29 '15 18:04

John Doucette


People also ask

How do you hash a mutable object in Python?

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.

What are immutable and mutable types list immutable and mutable types of Python?

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.

How do you check if something is mutable in Python?

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.

What are the examples of mutable and immutable in Python?

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.


2 Answers

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).

like image 96
Jonathan M Davis Avatar answered Sep 23 '22 19:09

Jonathan M Davis


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

like image 23
Kozzi11 Avatar answered Sep 21 '22 19:09

Kozzi11