Why are Julia strings immutable?

See the explanation here: Why are strings immutable (historical records request)

Julia doesn’t implement strings as arrays of characters

It doesn’t, it implements them internally as arrays of bytes in a UTF-8 encoding (but this internal array is hidden in Julia internals so that you cannot mutate it). This means that ASCII characters take only 1 byte per character, but other Unicode characters may take 2–4 bytes. In contrast, implementing strings as arrays of characters would take 4 bytes for every character for not much practical benefit.

(You might object that arrays of characters would allow you to query the n-th character quickly, or substitute one character for another in-place, but these operations turn out to be not so useful as you might think in practice, in part because Unicode is much more complicated than most people realize. See also utf8everywhere.org for more discussion of string encoding and why UTF-8 is a choice that many people have adopted.)

The StringViews.jl package implements an AbstractString type that is also UTF-8, but which can be backed by a mutable array of bytes. With this type, one could conceivably implement an in-place filter! function on a StringView. (Deleting characters in-place during iteration is very straightforward in the UTF-8 encoding, despite the variable character width.)

(And, of course, it is perfectly possible to implement a new AbstractString subtype backed by an array of Char. The LegacyStrings.jl package includes a related string type in the UTF-32 encoding, which is an array of Unicode characters identified by codepoint. It’s not something that seems to get much practical usage, however.)

26 Likes