[ruby-core:124410] [Ruby Feature#8948] Frozen regex
Issue #8948 has been updated by Eregon (Benoit Daloze). matz (Yukihiro Matsumoto) wrote in #note-35:
I still do not see enough benefit in putting in incompatible changes. Even if we can rewrite all the gems, we will still have incompatibility issues in user applications.
It seems extremely unlikely that applications subclass Regexp, or add instance variables to Regexps (which already doesn't work for literal Regexps). I would expect something like maybe 1 application in a million do that. The benefits are: * Non-literal Regexps are frozen too, which means they can be used with Ractor. * Simplicity for the semantics and users: all Regexps are frozen, not "some Regexps are frozen" and some not, when the internal state of a Regexp has always been immutable anyway. * Improved compatibility between Ruby implementations. The cons are: * A few gems out of ~188000 gems seem affected. It's not complicated to update the code to avoid relying on mutable Regexps (there is no need to subclass Regexp, one can just delegate the relevant methods). * A few applications might be affected. * In either case, I expect this potential incompatibility won't be noticed by 99.9+% users. ---------------------------------------- Feature #8948: Frozen regex https://bugs.ruby-lang.org/issues/8948#change-115928 * Author: sawa (Tsuyoshi Sawada) * Status: Assigned * Assignee: Eregon (Benoit Daloze) * Target version: 4.1 ---------------------------------------- =begin I see that frozen string was accepted for Ruby 2.1, and frozen array and hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel there is even more use case for a frozen regex, i.e., a regex literal that generates a regex only once. It is frequent to have a regex within a frequently repeated portion of code, and generating the same regex each time is a waste of resource. At the moment, we can have a code like: class Foo RE1 = /pattern1/ RE2 = /pattern1/ RE3 = /pattern1/ def classify case self when RE1 then 1 when RE2 then 2 when RE3 then 3 else 4 end end end but suppose we have a frozen `Regexp` literal `//f`. Then we can write like: class Foo def classify case self when /pattern1/f then 1 when /pattern1/f then 2 when /pattern1/f then 3 else 4 end end end =end -- https://bugs.ruby-lang.org/
participants (1)
-
Eregon (Benoit Daloze)