realworldocaml/book

Strings are immutable now

prekel opened this issue · 1 comments

TLDR: Imperative Programming, Strings says that strings are mutable

I was very surprised when I read Imperative Programming, Strings because I thought that strings in OCaml are immutable, but didn't check it in utop and left it as an interesting fact. But after reading Jérôme Vouillon's and Vincent Balat's "From Bytecode to Javascript: the Js_of_ocaml Compiler" I find out that JavaScript strings are immutable and therefore js_of_ocaml has their own implementation of strings. And afterward, I realize that ReScript uses JS's string representation. While investigating it, I found that there were changes in the OCaml compiler after that fragment was written which make strings immutable by default:

  • OCaml 4.02.0 (29 Aug 2014): Separation between read-only strings (type string) and read-write byte sequences (type bytes). Activated by command-line option -safe-string.
  • OCaml 4.06.0 (3 Nov 2017): ocaml/ocaml#1252: The default mode is now safe-string, can be overridden at configure time or at compile time.
  • OCaml 4.10.0 (21 February 2020): ocaml/ocaml#1859: enforce safe (immutable) strings by removing the -unsafe-string option by default.

So, I suggest rewriting Imperative Programming, Strings for bytes and to mention bytes in Memory Representation of Values, String Values.

Oh man, that's quite an oversight. Thanks for the catch! We'll fix it.