Add String#f method as shortcut for #freeze
We have String#b to create a binary-encoded String, and we have the "f" suffix (going away, hopefully) and the "literal".freeze optimization (#8992). I think it would be reasonable to add String#f as a shorter method for producing a frozen string.
If String#f is added the "literal".freeze optimization could be dropped in favor of "literal".f. This would provide something very close to the original "literal"f syntax but in a backward-compatibility-friendly way (class String; alias f freeze; end).
Updated by charliesome (Charlie Somerville) over 7 years ago
I feel really negatively about optimizing only String#f and not String#freeze. You could make the argument that it's backwards compatible, but in reality it means every bit of code that wants to use this optimization needs to monkey patch its own String#f.
Otherwise I support adding String#f as an alias for String#freeze and removing f-suffix syntax.
Updated by headius (Charles Nutter) over 7 years ago
- Assignee set to matz (Yukihiro Matsumoto)
The change to optimize String#freeze has landed for #8992, which compiler-optimizes String#freeze. The only remaining decision is whether to also add #f with the same optimization.
matz likes the idea of String#f. I'm happy either way, since #8992 landed, but a shorter method would be nice. With String#f acting like "str"f, we'd only be one character more verbose than the original syntax.
I believe matz needs to decide this one.
Updated by normalperson (Eric Wong) over 7 years ago
Fwiw, I am against adding aliases to the same method,
especially extremely short names which are hard to search for.
It is confusing and increases overhead for reading/following code.
Easy-to-write code is not necessary easy-to-read.
(speaking as a sometimes Perl hacker)