ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matthew@kerwin.net.au
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95738] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@
Date: Thu, 07 Nov 2019 04:42:01 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82557.20191107044201.8042bfd0570d6b6d@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16295.20191105134410@ruby-lang.org

Issue #16295 has been updated by phluid61 (Matthew Kerwin).


For what it's worth, I'm not *against* `#dedup` per se.  `-@` is great for signalling a frozen literal, but in the context at hand the method is more likely to be used to deduplicate a derived value.

What about adding a parameter to an existing method? `some_str.freeze(dedup: true)`

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

* 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 `<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:[~2019-11-07  4:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16295.20191105134410@ruby-lang.org>
2019-11-05 13:44 ` [ruby-core:95702] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@ jean.boussier
2019-11-05 16:33 ` [ruby-core:95704] " shevegen
2019-11-05 20:56 ` [ruby-core:95707] " matthew
2019-11-05 21:12 ` [ruby-core:95708] " eregontp
2019-11-06 10:59 ` [ruby-core:95722] " jean.boussier
2019-11-06 20:45 ` [ruby-core:95728] " matthew
2019-11-07  0:31 ` [ruby-core:95733] " XrXr
2019-11-07  3:05 ` [ruby-core:95734] " matthew
2019-11-07  3:56 ` [ruby-core:95735] " XrXr
2019-11-07  4:42 ` matthew [this message]
2019-11-07 16:09 ` [ruby-core:95745] " daniel
2019-11-12 16:35 ` [ruby-core:95817] " jean.boussier
2019-11-20 10:16 ` [ruby-core:95894] " zn

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-82557.20191107044201.8042bfd0570d6b6d@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).