Issue #16047 has been updated by Eregon (Benoit Daloze).

Assignee set to matz (Yukihiro Matsumoto)

I was also surprised that dynamic strings are also frozen, since the gain there seems minimal.
`"a#{b}c"` is conceptually the same as `"a".dup << b.to_s << "c"` and so freezing that seems artificial and not needed since there was mutation in the first place.
Also, it's basically one more mutation to the resulting String by changing its frozen flag, that would otherwise not be needed.

Interestingly enough, dynamic strings are not frozen currently in TruffleRuby (not intentional), and that seems to have caused no compatibility issue so far.
So if we went with this, I think it would be very compatible.

We use `# frozen_string_literal: true` for all core library Ruby files in TruffleRuby (18k SLOC, not typical application code though).
The majority of dynamic strings seem to be used for exception messages and #inspect.
Exception messages are mutable in MRI but we decided `+""` or `"".dup` are too ugly and so we keep them frozen when they are static string literals, which seems fine for compatibility so far.
OTOH, results of `#inspect` and other core methods are more commonly mutated by the user, and so there we would have to use `.dup` or `+" #{...} "` quite a few places.
I agree with you, I intuitively expect that dynamic strings are not frozen.

----------------------------------------
Misc #16047: Reconsider impact of frozen_string_literal on dynamic strings
https://bugs.ruby-lang.org/issues/16047#change-80400

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
The rationale for introducing `frozen_string_literal` was because rubyists were starting to litter their code with `"".freeze` for optimization, and it's ugly.

But by using frozen_string_literal we introduce the opposite problem: we must litter the code with `"".dup` in order to have mutable strings, and it's ugly.

The rationale for freezing all strings including dynamic was because it's [easy to explain]
(https://docs.google.com/document/u/1/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub)
This may be true, but at the expense of making it cumbersome to use. And also completely useless for memory optimization.

In my personal experience using frozen_string_literal, I find that static strings are usually ok to freeze without changing anything else, but that freezing dynamic strings often create bugs that require `+""` or `"".dup` boilerplate to circumvent. So in the end I found myself stopping regular use of that feature, since it's more trouble than it's worth.

As such I'd like to ask other rubyists how has been their experience with **actually using** frozen_string_literal on a day-to-day basis; if my experience is unique or common. Thank you for sharing your thoughts.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>