← Back to context

Comment by superjan

4 days ago

It’s true that many languages have immutable strings, but for a web-focused scripting language it makes sense to default to mutable strings. I think the comment is about that you now need to choose mutable vs immutable, and that is framed as a consequence of broader adoption. Which is a development I have also seen before.

> for a web-focused scripting language it makes sense to default to mutable strings

Why do you say that? I would say the opposite.

  • Because there’ll likely be a lot of string concatenation in a language whose job is to output HTML. Adding a single char to a 1k string will require allocating a modified copy, in mutable strings it’s just appending in a preallocated buffer.

    • If you are using string concatenation to build HTML in your application layer, you are fundamentally doing it wrong.

      Ruby isn’t making all strings immutable here. Just string literals. You are free to allocate mutable strings that can be appended to, to your heart’s content. It is extremely rare that modifying a literal is intended behavior, since their contents are permanently persisted throughout the lifetime of your program. With your example, this would be like having one shared global buffer for your final document.

Ruby is a web-focused scripting language in the same sense that Python is an AI-focused scripting language.

JavaScript is much more of a "web-focused scripting language" than Ruby is, and it is quite happy with immutable strings (only).

> I think the comment is about that you now need to choose mutable vs immutable, and that is framed as a consequence of broader adoption.

Ruby has also had immutable (frozen) strings for a very long time, so you've always had the choice. What is changing is that string literals are (eventually) going to migrate from "mutable with a strong-encouraged file level switch to make them immutable" to "immutable".

> for a web-focused scripting language

Ruby is not a web focused scripting language.