[#107430] [Ruby master Feature#18566] Merge `io-wait` gem into core IO — "byroot (Jean Boussier)" <noreply@...>
Issue #18566 has been reported by byroot (Jean Boussier).
22 messages
2022/02/02
[ruby-core:107671] [Ruby master Feature#16295] Chainable aliases for String#-@ and String#+@
From:
"Eregon (Benoit Daloze)" <noreply@...>
Date:
2022-02-20 12:39:28 UTC
List:
ruby-core #107671
Issue #16295 has been updated by Eregon (Benoit Daloze).
Exactly, and so `+@` is already good enough for `buffer = +"literal"`, and `buffer = "literal".dup` is fine too.
That's one of the rare cases where we know it's safe to reuse the String if it's already mutable.
So I think the use-cases for `.mutable`/`.mut` are very rare, either `+@` works already fine, or `.dup` should really be used instead for the safer semantics.
A third example from the original discussion would be `foo.to_s.dup << "..."` vs `+(foo.to_s) << "..."`.
The idiomatic way for concatenating would of course be simply `"#{foo}..."`, which doesn't need to know about mutability.
If some other mutable operation is needed, then `.dup` is much safer than `+@`, rather than relying on all `.to_s` creating a new String and never returning a cached mutable string (likely to not hold for a number of gems out there).
----------------------------------------
Feature #16295: Chainable aliases for String#-@ and String#+@
https://bugs.ruby-lang.org/issues/16295#change-96590
* 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/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>