From: matthew@... Date: 2019-11-07T03:05:46+00:00 Subject: [ruby-core:95734] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@ Issue #16295 has been updated by phluid61 (Matthew Kerwin). alanwu (Alan Wu) wrote: > I like `dedup` too. `-@` was introduced to expose deduplication in the first place. #11782 : > Specification: > > * `+'foo'` returns modifiable string. > * `-'foo'` returns frozen string (because wasters will freeze below 0 degree in Celsius). The optimisations aren't part of the original specification. In fact, it was all about adding `+@`, because at the time all string literals were intended to be frozen (and `-@` was meant to do nothing.) The deduplication came in #13077, and it was retrofit to `-@` specifically because there was no better name for the method. `fstring` was the original proposal, because it invokes `rb_fstring`. The 'f' stands for 'frozen', by the way. > Usages I've seen all have to do with memory concerns. You wouldn't call it just to get a frozen string, you care far more that it can deduplicate. I use `-"string"` because it's easier to type than `"string".freeze`, and both `-@` and `+@` are nice, clear signals of intention when I initialise a string; one is frozen, one is thawed. Deduplication is nice, but not my primary concern. ---------------------------------------- Feature #16295: Chainable aliases for String#-@ and String#+@ https://bugs.ruby-lang.org/issues/16295#change-82553 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Original discussion https://bugs.ruby-lang.org/issues/16150?next_issue_id=16147&prev_issue_id=16153#note-40 In #16150, @headius raised the following concern about `String#-@` and `String#+@`: headius (Charles Nutter) wrote: > > Not exactly, -@ and +@ makes this much simpler > > I do like the unary operators, but they also have some precedence oddities: > > ``` > >> -"foo".size > => -3 > >> (-"foo").size > => 3 > ``` > > And it doesn't work at all if you're chaining method calls: > > ``` > >> +ary.to_s.frozen? > NoMethodError: undefined method `+@' for false:FalseClass > from (irb):8 > from /usr/bin/irb:11:in `
' > ``` > > But you are right, instead of the explicit `dup` with possible freeze you could use `-` or `+` on the result of `to_s`. However it's still not safe to modify it since it would modify the original string too. After working for quite a while with those, I have to say I agree. They very often force to use parentheses, which is annoying, and an indication that regular methods would be preferable to unary operators. In response @matz proposed to alias them as `String#+` and `String#-` without arguments: > How about making String#+ and #- without argument behave like #+@ and #-@ respectively, so that we can write: > > ``` > "foo".-.size > ary.+.to_s.frozen? > ``` My personal opinion is that descriptive method names would be preferable to `+/-`: > IMHO `.-` and `.+` is not very elegant. Proper method names explaining the intent would be preferable. > > - `-@` could be `dedup`, or `deduplicate`. > - `+@` could be `mutable` or `mut`. -- https://bugs.ruby-lang.org/ Unsubscribe: