From: "danh337 (Dan H)" Date: 2022-02-21T19:04:23+00:00 Subject: [ruby-core:107691] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@ Issue #16295 has been updated by danh337 (Dan H). Eregon (Benoit Daloze) wrote in #note-19: > > .dup is not quite as good, as it always allocates a copy. > > It creates a new String instance, which is what one needs to guarantee safe mutation without affecting other parts of the program. > Hence, `+@` should rarely be used (only if we know where all the strings passed to this method come from and it's OK to mutate them). > `.dup` does not copy the actual bytes until mutated, because Strings are copy-on-write. > > > how is -@ different from .freeze ? The meaning of these seems very much the same. > > `-@` interns and might return a different String instance, `.freeze` does not intern and always returns the receiver. I see you are talking about the internal workings of the code for these, but the semantics is more important. If I want to use a method name to have the same effect as `-"foo"` I do `"foo".freeze`. There isn't really another way and no other way is needed. If I want to use a method name for `+"foo"` you say to use `"foo".dup`, but semantically that doesn't work well. Does anybody else say `.dup` is the best alternative to `.+@`? I'm sorry I do not agree. Is adding a `.dedup` method when we have `.freeze` really the final decision here? I guess if I'm the only objection then so be it. And it seems odd to just close this when there are some open questions. ---------------------------------------- Feature #16295: Chainable aliases for String#-@ and String#+@ https://bugs.ruby-lang.org/issues/16295#change-96609 * Author: byroot (Jean Boussier) * Status: Closed * Priority: Normal ---------------------------------------- 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: