ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen?
@ 2020-08-05 20:06 bughitgithub
  2020-08-05 20:25 ` [ruby-core:99487] " merch-redmine
                   ` (21 more replies)
  0 siblings, 22 replies; 23+ messages in thread
From: bughitgithub @ 2020-08-05 20:06 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been reported by bughit (bug hit).

----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99487] [Ruby master Misc#17104] Why are interpolated string literals frozen?
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
@ 2020-08-05 20:25 ` merch-redmine
  2020-08-05 20:58 ` [ruby-core:99488] " bughitgithub
                   ` (20 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: merch-redmine @ 2020-08-05 20:25 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Closed

Other people have felt the same way, including me (see https://bugs.ruby-lang.org/issues/11473#note-7 and https://bugs.ruby-lang.org/issues/8976#note-67). However, @matz decided during the November 2015 developer meeting that they should be frozen for simplicity, see https://docs.google.com/document/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub

----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104#change-86943

* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99488] [Ruby master Misc#17104] Why are interpolated string literals frozen?
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
  2020-08-05 20:25 ` [ruby-core:99487] " merch-redmine
@ 2020-08-05 20:58 ` bughitgithub
  2020-08-05 21:05 ` [ruby-core:99489] " merch-redmine
                   ` (19 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: bughitgithub @ 2020-08-05 20:58 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99489] [Ruby master Misc#17104] Why are interpolated string literals frozen?
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
  2020-08-05 20:25 ` [ruby-core:99487] " merch-redmine
  2020-08-05 20:58 ` [ruby-core:99488] " bughitgithub
@ 2020-08-05 21:05 ` merch-redmine
  2020-08-05 21:10 ` [ruby-core:99490] " eregontp
                   ` (18 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: merch-redmine @ 2020-08-05 21:05 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by jeremyevans0 (Jeremy Evans).


If you would like the behavior changed, you should file a feature request for it, and add it to the agenda of a future developer meeting.

----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104#change-86945

* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99490] [Ruby master Misc#17104] Why are interpolated string literals frozen?
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (2 preceding siblings ...)
  2020-08-05 21:05 ` [ruby-core:99489] " merch-redmine
@ 2020-08-05 21:10 ` eregontp
  2020-08-05 21:18 ` [ruby-core:99491] " bughitgithub
                   ` (17 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-08-05 21:10 UTC (permalink / raw)
  To: ruby-core

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


FWIW I'd be +1 to make interpolated Strings non-frozen by default.
It's BTW still the behavior in TruffleRuby to this day, since it seems to cause no incompatibility and is convenient for writing the core library with Ruby code.

----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104#change-86946

* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99491] [Ruby master Misc#17104] Why are interpolated string literals frozen?
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (3 preceding siblings ...)
  2020-08-05 21:10 ` [ruby-core:99490] " eregontp
@ 2020-08-05 21:18 ` bughitgithub
  2020-08-05 21:26 ` [ruby-core:99492] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal merch-redmine
                   ` (16 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: bughitgithub @ 2020-08-05 21:18 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by bughit (bug hit).


Can't we just treat this as I feature request? The reasons are, it will reduce allocations, be more logical, less surprising and produce simpler code (when a mutable string is needed and you don't want extra allocations)

----------------------------------------
Misc #17104: Why are interpolated string literals frozen?
https://bugs.ruby-lang.org/issues/17104#change-86947

* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99492] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (4 preceding siblings ...)
  2020-08-05 21:18 ` [ruby-core:99491] " bughitgithub
@ 2020-08-05 21:26 ` merch-redmine
  2020-08-06  1:37 ` [ruby-core:99493] " daniel
                   ` (15 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: merch-redmine @ 2020-08-05 21:26 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Closed to Open
Subject changed from Why are interpolated string literals frozen? to Do not freeze interpolated strings when using frozen-string-literal
Tracker changed from Misc to Feature

We can treat this as a feature request.  I changed it from Misc to Feature and modified the Subject to be the feature you are requesting as opposed to a question.  The issue for the next developer meeting is at https://bugs.ruby-lang.org/issues/17041 if you want to add it to the agenda.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-86948

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99493] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (5 preceding siblings ...)
  2020-08-05 21:26 ` [ruby-core:99492] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal merch-redmine
@ 2020-08-06  1:37 ` daniel
  2020-08-06  9:23 ` [ruby-core:99496] " jean.boussier
                   ` (14 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: daniel @ 2020-08-06  1:37 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by Dan0042 (Daniel DeLorme).


+1
A while ago I opened ticket #16047 about the same thing.
Seems a lot of people don't like the current behavior so much.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-86950

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99496] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (6 preceding siblings ...)
  2020-08-06  1:37 ` [ruby-core:99493] " daniel
@ 2020-08-06  9:23 ` jean.boussier
  2020-08-06 10:12 ` [ruby-core:99497] " eregontp
                   ` (13 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: jean.boussier @ 2020-08-06  9:23 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by byroot (Jean Boussier).


> If you want to get a mutable string from an interpolated literal +"_#{method1}_", 2 allocation will be done instead of 1

Maybe the parser could understand `+"#{}"` and avoid that second allocation? The same way it understand `"".freeze`.

Because I understand the consistency argument. "All string literals are frozen" is much easier to wrap your head around than "All string literals are frozen except the ones that are interpolated".

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-86954

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99497] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (7 preceding siblings ...)
  2020-08-06  9:23 ` [ruby-core:99496] " jean.boussier
@ 2020-08-06 10:12 ` eregontp
  2020-08-06 11:10 ` [ruby-core:99501] " jean.boussier
                   ` (12 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-08-06 10:12 UTC (permalink / raw)
  To: ruby-core

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


byroot (Jean Boussier) wrote in #note-8:
> Because I understand the consistency argument. "All string literals are frozen" is much easier to wrap your head around than "All string literals are frozen except the ones that are interpolated".

I'd argue `"a#{2}c"` is not a string literal (`"a2c"` is a string literal).
It's actually syntactic sugar for `mutableEmptyStringOfSourceEncoding << "a" << 2.to_s << "c"`.
(+ `.freeze` everything in current semantics which feels unneeded)
Same as `42` is an integer literal, but `41 + 1` is not an integer literal.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-86955

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99501] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (8 preceding siblings ...)
  2020-08-06 10:12 ` [ruby-core:99497] " eregontp
@ 2020-08-06 11:10 ` jean.boussier
  2020-08-10 18:51 ` [ruby-core:99546] " bughitgithub
                   ` (11 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: jean.boussier @ 2020-08-06 11:10 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by byroot (Jean Boussier).


> I'd argue "a#{2}c" is not a string literal ("a2c" is a string literal).

I understand your point of view. However in my view what defines a literal, is the use of a specific syntax, so `""` in this case.

Same for hashes or arrays, `[1 + 2]` is a literal (to me), it might not be a "static" literal, but it is a literal nonetheless.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-86958

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99546] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (9 preceding siblings ...)
  2020-08-06 11:10 ` [ruby-core:99501] " jean.boussier
@ 2020-08-10 18:51 ` bughitgithub
  2020-08-11  6:22 ` [ruby-core:99551] " duerst
                   ` (10 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: bughitgithub @ 2020-08-10 18:51 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by bughit (bug hit).


> However in my view what defines a literal, is the use of a specific syntax, so ""

There is only one reason for freezing literals, to be able to intern them and reduce allocation. In fact the feature is poorly named, after the consequence(freezing), not the cause (interning).

Freezing strings that are not interned is pointless and counterproductive (it leads to more allocation in the name of less). 

A user who does not understand why literals are frozen is unlikely to even notice that interpolated ones are not. And if he does and wonders why, he will, if persistent, arrive at the reason (interning) and be better off for it. The foolish, in this case, consistency in the name of "simplicity" helps no one.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87005

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99551] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (10 preceding siblings ...)
  2020-08-10 18:51 ` [ruby-core:99546] " bughitgithub
@ 2020-08-11  6:22 ` duerst
  2020-08-11 15:57 ` [ruby-core:99560] " bughitgithub
                   ` (9 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: duerst @ 2020-08-11  6:22 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by duerst (Martin Dürst).


bughit (bug hit) wrote in #note-11:
> > However in my view what defines a literal, is the use of a specific syntax, so ""
> 
> There is only one reason for freezing literals, to be able to intern them and reduce allocation. In fact the feature is poorly named, after the consequence(freezing), not the cause (interning).

My understanding is that another reason is avoidance of alias effects. It's easy to write code that when cut down to the essential, does this:
```
a = b = "My string"
a.gsub!(/My/, 'Your')
```
and expects `b` to still be "My string". Freezing makes sure this throws an error.

(This is not an argument for or againts freezing interpolated strings.)


----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87013

* Author: bughit (bug hit)
* Status: Open
* 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:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99560] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (11 preceding siblings ...)
  2020-08-11  6:22 ` [ruby-core:99551] " duerst
@ 2020-08-11 15:57 ` bughitgithub
  2020-08-26  7:43 ` [ruby-core:99699] " akr
                   ` (8 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: bughitgithub @ 2020-08-11 15:57 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by bughit (bug hit).


> another reason is avoidance of alias effects

What you've shown is not another reason for freezing.

`a = b = "My string"`

both a and b refer to the same string object regardless of interning/freezing

there's no expectation that mutating it via `a` will not affect `b`

the interning scenario is:

```rb
a = "My string"
b = "My string"
a.gsub!(/My/, 'Your')
```

here there's an appearance of 2 string objects but when they are interned, there's only one, so mutation can not be allowed. As I said, interning is the feature, and it requires freezing.



----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87020

* Author: bughit (bug hit)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99699] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (12 preceding siblings ...)
  2020-08-11 15:57 ` [ruby-core:99560] " bughitgithub
@ 2020-08-26  7:43 ` akr
  2020-08-31  8:50 ` [ruby-core:99793] " matz
                   ` (7 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: akr @ 2020-08-26  7:43 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by akr (Akira Tanaka).


Non-freezing interpolated strings is good thing to reduce allocations.
I think it is possible if most developers understands difference of interpolated and non-interpolated strings.


----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87190

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99793] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (13 preceding siblings ...)
  2020-08-26  7:43 ` [ruby-core:99699] " akr
@ 2020-08-31  8:50 ` matz
  2020-08-31 19:28 ` [ruby-core:99802] " eregontp
                   ` (6 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: matz @ 2020-08-31  8:50 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by matz (Yukihiro Matsumoto).


OK. Persuaded. Make them unfrozen.

Matz.


----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87310

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99802] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (14 preceding siblings ...)
  2020-08-31  8:50 ` [ruby-core:99793] " matz
@ 2020-08-31 19:28 ` eregontp
  2020-09-01 15:39 ` [ruby-core:99817] " mame
                   ` (5 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-08-31 19:28 UTC (permalink / raw)
  To: ruby-core

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

Assignee set to Eregon (Benoit Daloze)

I'll try to make a PR for this change.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87322

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99817] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (15 preceding siblings ...)
  2020-08-31 19:28 ` [ruby-core:99802] " eregontp
@ 2020-09-01 15:39 ` mame
  2020-09-01 18:18 ` [ruby-core:99822] " eregontp
                   ` (4 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: mame @ 2020-09-01 15:39 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by mame (Yusuke Endoh).


Eregon (Benoit Daloze) wrote in #note-17:
> I'll try to make a PR for this change: https://github.com/ruby/ruby/pull/3488

Thanks, I've given it a try.

I found `"foo#{ "foo" }"` frozen because it is optimized to `"foofoo"` at the parser.  What do you think?

```
$ ./miniruby --enable-frozen-string-literal -e 'p "foo#{ "foo" }".frozen?'
true
```

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87341

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99822] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (16 preceding siblings ...)
  2020-09-01 15:39 ` [ruby-core:99817] " mame
@ 2020-09-01 18:18 ` eregontp
  2020-09-02 14:15 ` [ruby-core:99840] " nobu
                   ` (3 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-09-01 18:18 UTC (permalink / raw)
  To: ruby-core

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


mame (Yusuke Endoh) wrote in #note-18:
> I found `"foo#{ "foo" }"` frozen because it is optimized to `"foofoo"` at the parser.  What do you think?

I guess that's semantically correct (besides the frozen status), since interpolation does not need to call `#to_s` for a String.
Since such code is very unlikely to appear in real code, I think it ultimately does not matter too much.

But if we can easily remove that optimization in the parser, I think it would be better, because this is an inconsistency (it makes it harder to reason about Ruby semantics & it breaks referential transparency) and optimizing `"foo#{ "foo" }"` seems to have no use in practice.
Could you point me to where the optimization is done in the parser if you found it? :)

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87347

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99840] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (17 preceding siblings ...)
  2020-09-01 18:18 ` [ruby-core:99822] " eregontp
@ 2020-09-02 14:15 ` nobu
  2020-09-03  0:29 ` [ruby-core:99858] " ko1
                   ` (2 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: nobu @ 2020-09-02 14:15 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by nobu (Nobuyoshi Nakada).


https://github.com/nobu/ruby/tree/unfrozen-literal

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87364

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99858] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (18 preceding siblings ...)
  2020-09-02 14:15 ` [ruby-core:99840] " nobu
@ 2020-09-03  0:29 ` ko1
  2020-09-03 14:26 ` [ruby-core:99877] " eregontp
  2020-09-15 19:35 ` [ruby-core:100015] " eregontp
  21 siblings, 0 replies; 23+ messages in thread
From: ko1 @ 2020-09-03  0:29 UTC (permalink / raw)
  To: ruby-core

Issue #17104 has been updated by ko1 (Koichi Sasada).


I found that freezing interpolated strings are help for Ractor programming with constants.

```ruby
class C
  i = 10
  STR = "foo#{i}"
end
```


----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87385

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:99877] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (19 preceding siblings ...)
  2020-09-03  0:29 ` [ruby-core:99858] " ko1
@ 2020-09-03 14:26 ` eregontp
  2020-09-15 19:35 ` [ruby-core:100015] " eregontp
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-09-03 14:26 UTC (permalink / raw)
  To: ruby-core

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


ko1 (Koichi Sasada) wrote in #note-21:
> I found that freezing interpolated strings are help for Ractor programming with constants.

Right, anything deeply frozen is helpful for Ractor.
But interpolated Strings are probably not that common.
I see an interpolated String much like an Array literals, and those are not frozen without an explicit `.freeze`.

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87404

* Author: bughit (bug hit)
* Status: Open
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [ruby-core:100015] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal
  2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
                   ` (20 preceding siblings ...)
  2020-09-03 14:26 ` [ruby-core:99877] " eregontp
@ 2020-09-15 19:35 ` eregontp
  21 siblings, 0 replies; 23+ messages in thread
From: eregontp @ 2020-09-15 19:35 UTC (permalink / raw)
  To: ruby-core

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


I merged https://github.com/ruby/ruby/pull/3488 so it's in time for preview1.
@nobu Could you decide if you think https://github.com/nobu/ruby/tree/unfrozen-literal is worth it?

----------------------------------------
Feature #17104: Do not freeze interpolated strings when using frozen-string-literal
https://bugs.ruby-lang.org/issues/17104#change-87566

* Author: bughit (bug hit)
* Status: Closed
* Priority: Normal
* Assignee: Eregon (Benoit Daloze)
----------------------------------------
I think the point of frozen string literals is to avoid needless allocations. Interpolated strings are allocated each time, so freezing them appears pointless. 

```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'
```




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

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2020-09-15 19:35 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-05 20:06 [ruby-core:99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? bughitgithub
2020-08-05 20:25 ` [ruby-core:99487] " merch-redmine
2020-08-05 20:58 ` [ruby-core:99488] " bughitgithub
2020-08-05 21:05 ` [ruby-core:99489] " merch-redmine
2020-08-05 21:10 ` [ruby-core:99490] " eregontp
2020-08-05 21:18 ` [ruby-core:99491] " bughitgithub
2020-08-05 21:26 ` [ruby-core:99492] [Ruby master Feature#17104] Do not freeze interpolated strings when using frozen-string-literal merch-redmine
2020-08-06  1:37 ` [ruby-core:99493] " daniel
2020-08-06  9:23 ` [ruby-core:99496] " jean.boussier
2020-08-06 10:12 ` [ruby-core:99497] " eregontp
2020-08-06 11:10 ` [ruby-core:99501] " jean.boussier
2020-08-10 18:51 ` [ruby-core:99546] " bughitgithub
2020-08-11  6:22 ` [ruby-core:99551] " duerst
2020-08-11 15:57 ` [ruby-core:99560] " bughitgithub
2020-08-26  7:43 ` [ruby-core:99699] " akr
2020-08-31  8:50 ` [ruby-core:99793] " matz
2020-08-31 19:28 ` [ruby-core:99802] " eregontp
2020-09-01 15:39 ` [ruby-core:99817] " mame
2020-09-01 18:18 ` [ruby-core:99822] " eregontp
2020-09-02 14:15 ` [ruby-core:99840] " nobu
2020-09-03  0:29 ` [ruby-core:99858] " ko1
2020-09-03 14:26 ` [ruby-core:99877] " eregontp
2020-09-15 19:35 ` [ruby-core:100015] " eregontp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).