Commutativity in operators

In C #, there is no built-in concept of commutativity in a language. That is, if I define a simple Vector class, for example:

 public struct Vector { private readonly double x, y, z; ... public static Vector operator +(Vector v, double d) {...} } 

I still need to define the symmetric operator +(double d, Vector v) , or any expression of the form 2.1 * v will give a compilation error or fail at run time in a dynamic script.

My question is twofold: firstly, are there any languages ​​where you can define commutative operators without having to define as possible combinations of operands? In other words, are there languages ​​familiar with the concept of switching? And secondly, it’s ever been reviewed in C # and revised, since it’s really just syntactic sugar, to avoid a few extra keystrokes that really don’t add all that richness to the language (and most likely, an additional reserved word)?

+8
c # programming-languages
source share
1 answer

Are there any languages ​​where you can define commutative operators without having to define as possible combinations of operands?

I do not know a single one, but that does not mean that they do not exist.

Has it ever been reviewed in C # and revised since it is really just syntactic sugar to avoid a few extra keystrokes that don't really add all that richness to the language (and most likely an extra reserved word is needed)?

As far as I know, this has never been considered. This, of course, was never on the list of possible language improvements, and I don’t remember seeing it in the archive of design notes.

You make a sufficient argument against this function in your question. If I asked for additional criticism of the proposed function, I would indicate that many overloaded operators are not commutative, even when their mathematical colleagues are commutative. For example, the string complement is not explicitly commutative, although both operands are strings. And the same goes for the "add" delegate. And there are many other properties of operators that would be equally or more useful, which I would like to put before auto-switching.

+7
source share

All Articles