ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "danh337 (Dan H)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:107661] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@
Date: Fri, 18 Feb 2022 22:41:03 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-96575.20220218224102.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16295.20191105134410.7941@ruby-lang.org

Issue #16295 has been updated by danh337 (Dan H).


Eregon (Benoit Daloze) wrote in #note-16:
> `+@` is rarely safe to use (only if you know what allocated it and that it was never captured in another variable) as it might mutate an argument inplace, if that object is not frozen.
> In most cases, people actually want to use `.dup` and that already exists.
> 
> @danh337 `-@` is not the same as `freeze`, see discussion above.

`.dup` is not quite as good, as it always allocates a copy. The `.thaw` semantics would be to allocate a new copy only if the receiver is already frozen.

I realize that "unfreeze" does not exist, but I'm making the assumption (yes dangerous) that `+@` and `-@` on existing Strings do their best to avoid allocating new objects, and currently there is no method equivalent to do that for `+@`.

----------------------------------------
Feature #16295: Chainable aliases for String#-@ and String#+@
https://bugs.ruby-lang.org/issues/16295#change-96575

* Author: byroot (Jean Boussier)
* Status: Open
* 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 `<main>'
> ```
> 
> 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/

  parent reply	other threads:[~2022-02-18 22:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16295.20191105134410.7941@ruby-lang.org>
2021-03-30 21:12 ` [ruby-core:103113] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@ dan
2021-03-30 21:31 ` [ruby-core:103114] " dan
2021-03-30 21:52 ` [ruby-core:103115] " merch-redmine
2021-03-30 23:11 ` [ruby-core:103116] " eregontp
2022-02-18 22:41 ` danh337 (Dan H) [this message]
2022-02-18 22:48 ` [ruby-core:107662] " danh337 (Dan H)
2022-02-19 20:34 ` [ruby-core:107667] " Eregon (Benoit Daloze)
2022-02-19 20:39 ` [ruby-core:107668] " Eregon (Benoit Daloze)
2022-02-19 23:44 ` [ruby-core:107669] " zverok (Victor Shepelev)
2022-02-20  1:49 ` [ruby-core:107670] " phluid61 (Matthew Kerwin)
2022-02-20 12:39 ` [ruby-core:107671] " Eregon (Benoit Daloze)
2022-02-21  8:46 ` [ruby-core:107681] " byroot (Jean Boussier)
2022-02-21 10:57 ` [ruby-core:107683] " byroot (Jean Boussier)
2022-02-21 19:04 ` [ruby-core:107691] " danh337 (Dan H)
2022-02-21 19:35 ` [ruby-core:107695] " danh337 (Dan H)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-96575.20220218224102.7941@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).