[ruby-core:71298] [Ruby trunk - Feature #8976] [Open] file-scope freeze_string directive

From: ko1@...
Date: 2015-11-02 10:58:09 UTC
List: ruby-core #71298
Issue #8976 has been updated by Koichi Sasada.

Status changed from Closed to Open

Quoted from=20
log: https://docs.google.com/document/d/1axnQv1A2SdRExw--_RzXXJAPrRyvN7MCIB=
0WrKcZaSE/pub
of https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20151021J=
apan

```
Decide whether -*- should be required for frozen_string_literal magic comme=
nts [Feature #8976]

naruse: vim=E2=80=99s indicator is =E2=80=9Cvim: =E2=80=A6=E2=80=9D http://=
vim.wikia.com/wiki/Modeline_magic
nobu: should be have an indicator of pragrma.
matz: why?
nobu: to avoid misunderstanding with comments.
matz: nobody care about that.
nobu: line should not include any other information.
matz: i agree with it.

conclusion:
accept pragma without indicators
do not accept pragma in the comment (do not skip any words before pragma ke=
ywords)

matz: i don=E2=80=99t want to recommend indicators.
nobu: sould we remove indicators?
akr: should remain indicators to keep compatibility.
ko1: only coding pragma?
nobu: we have warn-indnent pragmra.
naruse: warn-indent is in 2010
nobu: i=E2=80=99ll make it to keep compatibility.

conclusion:
keep compatibility for current indicator (for any pragma)

naruse: should we put pragmas in one line? or multi-lines?
akr: there are possibilities to increase pragma, so that shold accept multi=
-lines.
matz: i do not care about multi-lines.
akr: people may want to write copyright early.
naruse: people may want to write a comment for pragmas

conclusion:
accept multi-lines at the beggining of programming.
accept comments between pragmas.

akr: how about dynamic strings =E2=80=9Cfoo#{bar}baz=E2=80=9D?
nobu: how about =E2=80=9C#{=E2=80=98baz=E2=80=99}=E2=80=9D?
ko1: it is easy to understand all =E2=80=9C...=E2=80=9D is frozen. Shugo-sa=
n=E2=80=99s first comment is for =E2=80=9Ccompatibility=E2=80=9D.
matz: i agree to=20

matz: i have another idea +=E2=80=9C...=E2=80=9D -> mutable, -=E2=80=9D...=
=E2=80=9D -> immutable. it is more clean than =E2=80=9C...=E2=80=9D.freeze.=
 how about that, instead of the magic comment? i don=E2=80=99t care about p=
ull-requests, but care about the representation of source code.
akr: =E2=80=A6
matz: I don=E2=80=99t like magic comment, so that I hesitate to introduce i=
t.

a_matsuda: + and - seems string operation.  << -=E2=80=9Dfoo=E2=80=9D can b=
e seen as a here document.
matz: I understand.  + and - is not good idea.

matz: another idea: make =E2=80=98=E2=80=99=E2=80=99foo=E2=80=99=E2=80=99=
=E2=80=99 frozen.  incompatibility is negligible.
matz: yet another idea: make single quoted strings frozen. This is incompat=
ible.

MISC:
akr: should we separate this topic to another ticket? this is =E2=80=9Chow =
to write a pragmra=E2=80=9D.
ko1: we should describe syntax of =E2=80=9Chow to write pragmas=E2=80=9D

matz: I don=E2=80=99t like pragmas but if it is required=20
ko1: this pragma is for experimental feature. if we decide to reject defaul=
t frozen string literal, then you remove this pragma, or remain this pragma?
matz: i will remain it.

deep_freeze
=E2=86=92 traversal framework?

inspect =E3=81=AB=E5=BC=95=E6=95=B0=E3=82=92=E6=B8=A1=E3=81=97=E3=81=9F=E3=
=81=84
=E2=86=92 inspect2? not clean.
naruse: On 1.9 I tried to implement such logic, but I gave  up and use Enco=
ding.default_internal/Encoding.default_external

```

Maybe there are several incomprehensible sentences.
Please ask that and continue to discuss.

Thanks,
Koichi


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

* Author: Akira Tanaka
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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 literal=
s 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" war=
ning.

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




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

In This Thread