From: "alexeymuranov (Alexey Muranov)" Date: 2012-11-22T07:50:06+09:00 Subject: [ruby-core:49835] [ruby-trunk - Bug #7331] Set the precedence of unary `-` equal to the precedence `-`, same for `+` Issue #7331 has been updated by alexeymuranov (Alexey Muranov). stomar (Marcus Stollsteimer) wrote: > Generally, adding "wrong" syntax just for consistency is IMO no option. > Well, when the wrong syntax is already present (`- a * b` is `(- a) * b`) i do not think #7328 would make the syntax as a whole "more wrong" (IMO it would even make it right in some sense). > But, looking over the thread again, I don't see your problem in the first place: > - 2 * 3 means (-1) * 2 * 3 = (- 2) * 3 = - (2 * 3), which all results in -6, so what's wrong with that? The question is about parsing, not about the arithmetic. There is (i believe) no Ruby specification saying that `(-a) * b` must produce the same result and side-effects as `-(a * b)`. If it was a part of Ruby specification, i wouldn't have reported this issue. > Furthermore, -2 * -2 is mathematically invalid syntax, you would have to write -2 * (-2), > but here also there is no ambiguity involved, - (2 * (-2)) = 4 = (-2) * (-2). In Ruby, `-a * -b` is allowed, and means `(-a) * (-b)`, not `-(a * (-b))`. Hence an ambiguity for an unsuspecting user. > The only thing that seems worth mentioning is that Ruby allows the shortcut 2 * - 3 for 2 * (-3). Yes, this is worth mentioning :). If there are other arguments, i think it would be better to continues the discussion in the thread for #7328. ---------------------------------------- Bug #7331: Set the precedence of unary `-` equal to the precedence `-`, same for `+` https://bugs.ruby-lang.org/issues/7331#change-33419 Author: alexeymuranov (Alexey Muranov) Status: Rejected Priority: Normal Assignee: Category: core Target version: ruby -v: 1.9.3 =begin I will not be surprised if this proposal is rejected, because the compatibility would be a mess, but i feel i need to start a discussion. This continues the discussion in #7328. To the best of my knowledge, in mathematics the unary minus has the same precedence as the binary one. However, in the expression - a * b Ruby computes first (({-a})), and then the product. This means that if i want (for whatever reason) to compute the expression in the natural order, i have to put the parentheses: - (a * b) which looks very strange to me. I would consider this a bug. =end -- http://bugs.ruby-lang.org/