Proposal for the strict mode
I propose to introduce a new magic comment for the ”strict mode” which is generally equivalent to that of other languages such as Perl or recent versions of ECMAScript.
In short, it’s a way to enable the cleaner subset of Ruby.
It’s a file scoped mode switching, restricting some of the language core features that are discouraged to use.
# strict: true
#=> enables the strict mode
# strict: false
#=> disables the strict mode (default)
- global variables
- class variables
- Perl-ish $vars
- constant redefinition
- method redefinition
- too dynamic features, such as eval, instance_eval, module_eval, class_eval, method_missing
- maybe some other features that are already being warned
- maybe some other features hat Matz wants to remove in the future
Plus, this includes frozen_string_literal.
I kind of regret frozen_string_literal
magic comment that I proposed to introduce in 2.3, because it looks ugly.
When strict: true
was given, frozen_string_literal
is also set to true
. When both strict
and frozen_string_literal
were given, frozen_string_literal
wins.
All strict mode checks should be done in parse and compile phase.
The biggest benefit that we can expect from the strict mode is optimization. By restricting those too broad scoped or too dynamic features, we will be able to simplify VM instructions. Also, this would be a help for other optimization approaches such as Vlad’s MJIT, shyouhei’s deopt, jruby, etc.
Warnings are basically just a guideline. Users can still ignore the warning messages and it still works. So the implementation can never assume that these evil features are not in use. With strict more, we can 100% assume cleaner subset of Ruby.