ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91556] [Ruby trunk Feature#15606] Precendence of -@ and +@
       [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
@ 2019-02-15  9:24 ` mail
  2019-02-15 10:23 ` [ruby-core:91559] [Ruby trunk Feature#15606] Precedence " nobu
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: mail @ 2019-02-15  9:24 UTC (permalink / raw)
  To: ruby-core

Issue #15606 has been reported by sos4nt (Stefan Schüßler).

----------------------------------------
Feature #15606: Precendence of -@ and +@
https://bugs.ruby-lang.org/issues/15606

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`-@` and `+@` seem to have issues with method chaining:

```ruby
i = 5

-i.negative?
# NoMethodError (undefined method `-@' for false:FalseClass)
```

here's another example:

```ruby
# frozen_string_literal: true

+'foo'.upcase!
# FrozenError (can't modify frozen String)
```

I know that I can fix this by adding parentheses, i.e. `(-i).negative?` and `(+'foo').upcase!` but it feels cumbersome.

Shouldn't the above work out of the box?

Unless I'm missing a crucial use case, the precedence for `-@` and `+@` should be changed.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:91559] [Ruby trunk Feature#15606] Precedence of -@ and +@
       [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
  2019-02-15  9:24 ` [ruby-core:91556] [Ruby trunk Feature#15606] Precendence of -@ and +@ mail
@ 2019-02-15 10:23 ` nobu
  2019-02-15 10:28 ` [ruby-core:91560] " mail
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: nobu @ 2019-02-15 10:23 UTC (permalink / raw)
  To: ruby-core

Issue #15606 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Feedback

Do you expect `i = -1; -i.abs` to return `+1`?

----------------------------------------
Feature #15606: Precedence of -@ and +@
https://bugs.ruby-lang.org/issues/15606#change-76822

* Author: sos4nt (Stefan Schüßler)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`-@` and `+@` seem to have issues with method chaining:

```ruby
i = 5

-i.negative?
# NoMethodError (undefined method `-@' for false:FalseClass)
```

here's another example:

```ruby
# frozen_string_literal: true

+'foo'.upcase!
# FrozenError (can't modify frozen String)
```

I know that I can fix this by adding parentheses, i.e. `(-i).negative?` and `(+'foo').upcase!` but it feels cumbersome.

Shouldn't the above work out of the box?

Unless I'm missing a crucial use case, the precedence for `-@` and `+@` should be changed.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:91560] [Ruby trunk Feature#15606] Precedence of -@ and +@
       [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
  2019-02-15  9:24 ` [ruby-core:91556] [Ruby trunk Feature#15606] Precendence of -@ and +@ mail
  2019-02-15 10:23 ` [ruby-core:91559] [Ruby trunk Feature#15606] Precedence " nobu
@ 2019-02-15 10:28 ` mail
  2019-02-15 10:39 ` [ruby-core:91561] " mail
  2019-02-16  6:02 ` [ruby-core:91574] " spinutids
  4 siblings, 0 replies; 5+ messages in thread
From: mail @ 2019-02-15 10:28 UTC (permalink / raw)
  To: ruby-core

Issue #15606 has been updated by sos4nt (Stefan Schüßler).


nobu (Nobuyoshi Nakada) wrote:
> Do you expect `i = -1; -i.abs` to return `+1`?

Yes, indeed.

The current behavior is quite surprising:
```ruby
i = -1

-1.abs #=> 1
-i.abs #=> -1
```

----------------------------------------
Feature #15606: Precedence of -@ and +@
https://bugs.ruby-lang.org/issues/15606#change-76823

* Author: sos4nt (Stefan Schüßler)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`-@` and `+@` seem to have issues with method chaining:

```ruby
i = 5

-i.negative?
# NoMethodError (undefined method `-@' for false:FalseClass)
```

here's another example:

```ruby
# frozen_string_literal: true

+'foo'.upcase!
# FrozenError (can't modify frozen String)
```

I know that I can fix this by adding parentheses, i.e. `(-i).negative?` and `(+'foo').upcase!` but it feels cumbersome.

Shouldn't the above work out of the box?

Unless I'm missing a crucial use case, the precedence for `-@` and `+@` should be changed.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:91561] [Ruby trunk Feature#15606] Precedence of -@ and +@
       [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-02-15 10:28 ` [ruby-core:91560] " mail
@ 2019-02-15 10:39 ` mail
  2019-02-16  6:02 ` [ruby-core:91574] " spinutids
  4 siblings, 0 replies; 5+ messages in thread
From: mail @ 2019-02-15 10:39 UTC (permalink / raw)
  To: ruby-core

Issue #15606 has been updated by sos4nt (Stefan Schüßler).


nobu (Nobuyoshi Nakada) wrote:
> Do you expect `i = -1; -i.abs` to return `+1`?

Yes, indeed. I'd expect `<something>.abs` to return a positive value.

```ruby
 1.abs #=> 1
-1.abs #=> 1

i = 1

 i.abs #=> 1
-i.abs #=> -1  <- not what I'd expect
```

----------------------------------------
Feature #15606: Precedence of -@ and +@
https://bugs.ruby-lang.org/issues/15606#change-76824

* Author: sos4nt (Stefan Schüßler)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`-@` and `+@` seem to have issues with method chaining:

```ruby
i = 5

-i.negative?
# NoMethodError (undefined method `-@' for false:FalseClass)
```

here's another example:

```ruby
# frozen_string_literal: true

+'foo'.upcase!
# FrozenError (can't modify frozen String)
```

I know that I can fix this by adding parentheses, i.e. `(-i).negative?` and `(+'foo').upcase!` but it feels cumbersome.

Shouldn't the above work out of the box?

Unless I'm missing a crucial use case, the precedence for `-@` and `+@` should be changed.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:91574] [Ruby trunk Feature#15606] Precedence of -@ and +@
       [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-02-15 10:39 ` [ruby-core:91561] " mail
@ 2019-02-16  6:02 ` spinutids
  4 siblings, 0 replies; 5+ messages in thread
From: spinutids @ 2019-02-16  6:02 UTC (permalink / raw)
  To: ruby-core

Issue #15606 has been updated by spinute (Satoru Horie).


Compatibility is essential in Ruby. So, we need strong evidence when we break it.

I do not think the current behavior is weird.

`<something>.abs` returns positive value, consistently.

A programmer who knows `-i.abs` is evaluated as `-(i.abs)` can get an expected result. As a note, `-` in `-1` is not an operator, so `-1.abs == (-1).abs == 1`. `-i.to_s` is also explained in the same way.

I agree that the behavior may be confusing for beginners.

----------------------------------------
Feature #15606: Precedence of -@ and +@
https://bugs.ruby-lang.org/issues/15606#change-76838

* Author: sos4nt (Stefan Schüßler)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`-@` and `+@` seem to have issues with method chaining:

```ruby
i = 5

-i.negative?
# NoMethodError (undefined method `-@' for false:FalseClass)
```

here's another example:

```ruby
# frozen_string_literal: true

+'foo'.upcase!
# FrozenError (can't modify frozen String)
```

I know that I can fix this by adding parentheses, i.e. `(-i).negative?` and `(+'foo').upcase!` but it feels cumbersome.

Shouldn't the above work out of the box?

Unless I'm missing a crucial use case, the precedence for `-@` and `+@` should be changed.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-02-16  6:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15606.20190215092417@ruby-lang.org>
2019-02-15  9:24 ` [ruby-core:91556] [Ruby trunk Feature#15606] Precendence of -@ and +@ mail
2019-02-15 10:23 ` [ruby-core:91559] [Ruby trunk Feature#15606] Precedence " nobu
2019-02-15 10:28 ` [ruby-core:91560] " mail
2019-02-15 10:39 ` [ruby-core:91561] " mail
2019-02-16  6:02 ` [ruby-core:91574] " spinutids

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).