From: "alexeymuranov (Alexey Muranov)" <redmine@...>
Date: 2012-11-14T00:07:45+09:00
Subject: [ruby-core:49308] [ruby-trunk - Feature #7336] Flexiable OPerator Precedence


Issue #7336 has been updated by alexeymuranov (Alexey Muranov).


trans (Thomas Sawyer) wrote:

> 
>   10.meters**2  => 100m

(10.meters)**2  should be 100.meters^2 in your notation.


> Rather then the `5m^2` expected. While parenthesis can be used, it so unnatural to this common notation that it puts a rather ugly black mark on creating a nice SI units system for Ruby at all.

I think it is a choice between simple rules and complex context-dependent rules.  Is the user expected to know the particular context he/she is working with, or should it be enough to know the standard Ruby parsing rules?

> 
> This particular problem could easily be solved by raising the precedence of ^ to that of **, which is why I suggested #6678 first --although that feels more like a band-aid. Controllable precedence feels like the right solution to me b/c others may ultimately have different needs.
> 

If it was up to me, and if there were no compatibility problems, i would vote for using `^` everywhere in the sense of exponentiation, with the highest precedence,  perhaps except for the unary minus (see my comment 19 in #7328). Related: #7322.
----------------------------------------
Feature #7336: Flexiable OPerator Precedence
https://bugs.ruby-lang.org/issues/7336#change-32865

Author: trans (Thomas Sawyer)
Status: Rejected
Priority: Normal
Assignee: 
Category: core
Target version: next minor


=begin
If Ruby classes could provide some means for redefining operator precedence, it would provide the flexibility useful to some DSL use-cases.

My particular application, for instance, is in an SI units system gem that could use `^` to mean power of the unit (e.g. 1.meter^3 would mean cubic meters). But to do that right the operator needs a higher precedence. I don't expect it to be something commonly used, obviously, but it certain use cases like mine it is practically essential.

I first suggested that (({#^})) be given a higher precedence and XOR get another operator in #6678. I was not surprised that it was rejected, but I figured it was the proper first step, before proposing this much broader feature request.

As for notation, I suppose the simplest means if to create class method that can move the precedence to a position relative to another, e.g.

    class Unit
      precedence :^, :**

Which is to say, move (({#^})) operator to a precedence above (({#**})).
=end



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