Issue #8976 has been updated by Yusuke Endoh.

Status changed from Closed to Open
Assignee set to Nobuyoshi Nakada

> Anyway it is orthogonal to frozen_string_literal itself.

I do NOT think so.

The current problem I concern is that "frozen_string_literal" requires `-*-`.  This feature is the key test that decides Ruby's feature.  If people fail to use this feature, it will lead to the wrong decision.  And it is dangerous at least in the current situation, because people are only familiar with "encoding" magic comment that does not require `-*-`.

I do NOT intend to discuss the format of magic comment in general.  I personally prefer no `-*-`.  But it is just my preference.  I don't feel a danger or problem, at least currently, because there is no other important kind of magic comments.  (I don't think that warn_indent is critical.)

-- 
Yusuke Endoh <mame / ruby-lang.org>

----------------------------------------
Feature #8976: file-scope freeze_string directive
https://bugs.ruby-lang.org/issues/8976#change-54344

* Author: Akira Tanaka
* Status: Open
* Priority: Normal
* Assignee: Nobuyoshi Nakada
----------------------------------------
Yesterday, we had a face-to-face developer meeting.
https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20131001Japan
Several committers attended.
matz didn't attended, though.  (This means this issue is not concluded.)

We believe we found a better way to freeze static string literals for
less GC pressure.
"static string literal" is a string literal without dynamic expression.

Currently, `f`-suffix, `"..."f`, is used to freeze a string literal to avoid
String object allocation.

There are several problems for `f`-suffix:

* The notation is ugly.
* Syntax error on Ruby 2.0.
  We cannot use the feature in version independent libraries.
  So, it is difficult to deploy.
* Need to modify for each string literal.
  This is cumbersome.

The new way we found is a file-scope directive as follows

  # freeze_string: true

The above comment at top of a file changes semantics of
static string literals in the file.
The static string literals will be frozen and always returns same object.
(The semantics of dynamic string literals is not changed.)

This way has following benefits:

* No ugly `f`-suffix.
* No syntax error on older Ruby.
* We need only a line for each file.

We can write version independent library using frozen static string literals as follows.

* Use the directive at top of the file: `# freeze_string: true`
  Older Ruby ignore this as a comment.
* Use `"...".dup` for strings to be modified.
  Older Ruby has small disadvantage: useless `dup` is called.

Note that the directive effects all static string literals regardless of
single quotes, double quotes, `%q`-string, `%qq`-string and here documents.
The reason that the directive is effective not only single quotes is
we want to use escape sequences such as `\n` in frozen string literals.

Also note that similar directive is already exist:

~~~
% ruby -w -e '
def m
  end
'
-e:3: warning: mismatched indentations at 'end' with 'def' at 2
% ruby -w -e '# -*- warn_indent: false -*-
def m
  end
'
~~~

The directive, `warn_indent: false`, disables "mismatched indentations" warning.

nobu implemented this feature in the meeting.
Please attach the patch, nobu.




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