Issue #7336 has been updated by trans (Thomas Sawyer).


@matz I understand your cautionary take. It certainly could be confusing if someone where to horse around with precedence, say in the Object class. But I doubt it would prove such a problem in practice b/c developers would be well aware of the potential "dangers" of such a thing. It would only  end up being used for very specific cases like my own. From a more general principle, that's always been my favorite thing about Ruby --it trusts developers to shoot themselves in the foot at their own discretion :)  

If it would help, I could see a warning be issued stating "precedence has been changed for class XYZ". Also, if refinements are here to stay, maybe it could be localized by them?

I realize this isn't a minor feature request, and it needs to be considered carefully, both in its effects to the language and it's implementation. But it could prove fairly powerful, especially in academic areas. Maybe we could give the issue some time to collect feedback at least? Perhaps there are some other strong use cases out there we are not aware.

----------------------------------------
Feature #7336: Flexiable OPerator Precedence
https://bugs.ruby-lang.org/issues/7336#change-32863

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/