[ruby-core:95704] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@
From:
shevegen@...
Date:
2019-11-05 16:33:52 UTC
List:
ruby-core #95704
Issue #16295 has been updated by shevegen (Robert A. Heiler).
I agree that + and - are not very elegant. They are short, so useful, but this is actually
the main reason why I prefer the much longer .dup instead, and don't use + and -.
I often prefer short english words/names, within reason. It is always a trade off of course.
Ruby often allows "both" styles.
> They very often force to use parentheses, which is annoying
I agree in general. Being able to omit parens is great. I personally use parens in
method definitions if there is at least one argument; other ruby users omit the parens
completely, which I can understand, even if I don't use that style. But more importantly
I agree that being able to decide whether to use parens or not is GREAT. In python you
are forced to use them, and I find this annoying. (I really think ruby is better than
python in many ways.)
To the suggestion itself for the names:
I think all of dedup, deduplicate, mutable or mut are a bit ... clumsy.
IF the question were SOLELY between:
dedup versus deduplicate
and
mut versus mutable
Then I think the shorter names would be a tiny bit better. But .dedup is not a great name,
and .mut is a bit confusing. .deduplicate seems too long, I actually typoed when I tried
to write it just now :) - .mutable is ... hmm. The name seems a bit more like .mutable?
to me, as a query method.
I am not sure that these names are great.
Perhaps we can come up with names that describe the behaviour, without
having to focus on + or -.
If I understand the problem correctly then the primary issue is to find good name
candidates? If so perhaps people can give some suggestions.
Perhaps some name with .freeze_* or something like that, or .unfreeze (not sure
here, I think we can not unfreeze, only freeze, so such a name may cause
confusion).
Actually we already have .dup which I assume is short for .duplicate. So perhaps
the methods could be centered around .dup.
.de_dup
.un_dup
.dup+
.dup- # ok ok that does not work but ...
.dup_plus
.dup_minus # clumsy too ...
.chain_dup # uhm ...
.dup_chain # sounds like a music song
.freeze_dup # no idea why this even came up ...
.duppity # just sounds good
Well - short break from finding silly names ...
If we look at the documentation, we have:
-----
+str → str (mutable)
If the string is frozen, then return duplicated mutable string.
If the string is not frozen, then return the string itself.
-str → str (frozen)
Returns a frozen, possibly pre-existing copy of the string.
The string will be deduplicated as long as it is not tainted, or has any instance variables set on it.
-----
So how about ...
.frozen_copy
.frozen_or_copy
Actually, reading the documentation, .dedup seems to be ok:
.dedup
Even if the name is not perfect, it may be better than not
having an alternative.
I can't really think of a great name though. Perhaps others can
give some more ideas.
----------------------------------------
Feature #16295: Chainable aliases for String#-@ and String#+@
https://bugs.ruby-lang.org/issues/16295#change-82490
* 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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>