[ruby-core:99488] [Ruby master Misc#17104] Why are interpolated string literals frozen?
From:
bughitgithub@...
Date:
2020-08-05 20:58:44 UTC
List:
ruby-core #99488
Issue #17104 has been updated by bughit (bug hit).
If you want to get a mutable string from an interpolated literal `+"_#{method1}_"`, 2 allocation will be done instead of 1, if it weren't pointlessly frozen.
In this case a feature designed to reduce allocations is producing more allocations. Behavior that's counter-intuitive and illogical and acting counter to its intent, is not simple.
This happens to be something that can be changed without breaking anything. Can it get a second look?
----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104#change-86944
* Author: bughit (bug hit)
* Status: Closed
* Priority: Normal
----------------------------------------
```rb
#frozen_string_literal: true
def foo(str)
"#{str}"
end
fr1 = 'a'
fr2 = 'a'
fr1_1 = foo(fr1)
fr2_1 = foo(fr2)
puts fr1.__id__, fr2.__id__, fr1_1.__id__, fr2_1.__id__
puts fr1_1 << 'b'
```
Isn't the point of frozen literals to avoid needless allocations? But interpolated strings are allocated each time, so freezing appears pointless.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>