Why are strings immutable (historical records request)

At some point it was decided that Strings would be immutable in Julia. Is there a record of that conversation (e.g. a mailing list or GitHub thread)?

Failing that, is there a record of the conversations that led core language designers to decide to make strings immutable in another programming language?

Not quite what you’re looking for, but a starting point: String should be officially immutable & "abc" === "abc" · Issue #22193 · JuliaLang/julia · GitHub


I think the person who knows the history of this decision best may also be @ScottPJones !

1 Like

See https://groups.google.com/g/julia-dev/c/-u4k0xX5ziQ/m/jEV-Q2PBmw0J … strings were treated as immutable from the earliest days of Julia, even though the language didn’t enforce this at first.


I can’t find the origin in emails, but this was my decision, effected by not providing any mutating APIs to strings. Here’s an older more direct explanation of why from 2012:

There’s an important psychological distinction between things we think of as mutable versus immutable: values that are defined by their values should be immutable, whereas values that contain other values but have an independent identity can be mutable. Numbers are a prototypical example of being defined by their values — if you change the imaginary part of a complex number, you have a different number, not the same number altered (it sounds weird to even talk about “altering” a number). Arrays are the prototypical containers: change the contents of an array all you want and it still has an independent identity (psychologically).

The reason I believe strings are so dangerous as mutable values in high-level languages is that we think of them as being defined by their value: the string “foo” and “fox” are simply different values, regardless of whether the former was altered into the latter using the same memory. However, as a legacy from the implementation and the fact that in C they are just arrays of bytes, a low-level view of them is as containers. In C that’s natural and fairly unsurprising because you’re used to thinking about the actual memory buffer that represents a string. In a higher level language, it’s really, really surprising when a string changes value because you passed it to some other function. Some of the nastiest bugs I’ve ever had to find were due to this.


10 posts were split to a new topic: Is Julia well-suited for string manipulation?