const?? When and why? This is ugly!

bearophile bearophileHUGS at lycos.com
Sat Mar 7 05:57:31 PST 2009


Walter Bright:

This is an interesting topic.
I like immutability, but sometimes I also like mutability.

>languages that are considered very good at dealing with strings (like Perl) use immutable strings. The fascinating thing about strings in such languages is: "Nobody notices they are immutable, they just work."<

Languages that have immutable strings often have:
- "String interning", to improve performance...
- A good garbage collector, to cope with the increased allocation-deallocation traffic.
- Sometimes the garbage collector is able to see that two unrelated strings are equal, and keep only one of them. Experiments have shown this reduces a lot the memory used by many Java programs.
- Strings often keep their hash value stored beside them, so it's computed only once, the first time you actually need the hash value (this also means the hash value is initialized to an unvalid value).

People notice such strings are immutable. Usually it's fine, but once in a while it's a pain.

Note that in Python you usually try to avoid looping on single chars because it's a too much slow thing to do, so you try to use string methods and regular expressions as much as possible. But I like a lower level language because it gives me the *freedom* to read and process the single chars efficiently. (Python is implemented in C, and writing the Python interpreter itself with a language that uses immutable strings only is probably a pain).

Such languages like Python also always offer you an escape, for example in the standard library of Python there is a mutable char array (Python3 is different, it has as built-ins immutable unicode strings + mutable arrays of bytes + maybe an immutable array of bytes):

This is Python 2.5:

>>> from array import array
>>> # mutable array of chars
>>> s = array("c", "Hello")
>>> s
array('c', 'Hello')
>>> s[2] = "X"
>>> s
array('c', 'HeXlo')
>>> # mutable array of unicode chars
>>> t = array("u", u"Hello")
>>> t
array('u', u'Hello')
>>> t[1]= u"Y"
>>> t
array('u', u'HYllo')


Also note that Ruby, that is a very good language for string processing, allows mutable strings. So the situation isn't as clear cut as you think.

To compare such matters across languages you can also take a look at this page:
http://merd.sourceforge.net/pixel/language-study/various/mutability-and-sharing/


>After all, it never occurs to anyone to think that the integer 123 could be a "mutable" integer and perhaps be 133 the next time you look at it.<

Because they are small numbers. With the multi-precision GMP library you can mutate numbers in place because this becomes useful when you manage huge numbers.

Note that there are Python bindings for GMP, they manage numbers in an immutable way to respect the Python style, but it's not much efficient, see explanation here in the middle:
http://gmpy.sourceforge.net/


>The way to do strings in D is to have them be immutable. If you are building a string by manipulating its parts, start with mutable, when finished then convert it to immutable and 'publish' it to the rest of the program.<

Seems acceptable.


>you'll find you never need to defensively dup the string "just in case" and things just seem to naturally work out.<

If you put strings in an associative array as keys, you usually want them to be immutable to keep their correct place in the hash and avoid big troubles.
For such purpose Python has mutable and immutable arrays (named list and tuple), where you can only use tuples as dictionary keys.
So built-in associative arrays of D too may appreciate immutable arrays more :-)

Bye,
bearophile



More information about the Digitalmars-d mailing list