From: "enebo (Thomas Enebo)" Date: 2013-10-04T00:10:26+09:00 Subject: [ruby-core:57641] [ruby-trunk - Feature #8976] file-scope freeze_string directive Issue #8976 has been updated by enebo (Thomas Enebo). naruse (Yui NARUSE) wrote: > enebo (Thomas Enebo) wrote: > > I think having a pragma at the top of the file will be much more error prone than the f-syntax. As a file grows, the ability to notice you are in a frozen string file goes down. It would have been great if Ruby had started immutable strings by default but that ship has sailed, I think having some files be immutable will be confusing. > > Enhance your IDE. It is an answer but one I think is not acceptable (obviously that is only my opinion). > > > Are we sure we cannot find a nicer syntax for frozen strings: %f{hello, I am frozen}? > > Almost all of the idea to add new syntax break 2.0 compatibility, and it makes adoption of frozen strings slow. > It is the motivation of this suggestion. Yeah. ko1 talked to me yesterday about this. I have been trying to think of other ways to not break backwards compatibility and have not thought of anything. It is possible to put out a PL to 2.0 to add the syntax but obviously a decision like that is a big one. Older 2.0 libraries will not be able to read it. OTOH, MRI has been periodically putting out security fixes so perhaps a syntax error to force an upgrade is not a bad thing? ---------------------------------------- Feature #8976: file-scope freeze_string directive https://bugs.ruby-lang.org/issues/8976#change-42257 Author: akr (Akira Tanaka) Status: Open Priority: Normal Assignee: Category: Target version: current: 2.1.0 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. -- http://bugs.ruby-lang.org/