In C#, there is not in-built notion of commutativity in the language. That is if I define a simple Vector
class for instance:
public struct Vector
{
private readonly double x, y, z;
...
public static Vector operator +(Vector v, double d) {...}
}
I will 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 runtime in a dynamic scenario.
My question is two-fold: First, are there any languages out there where you can define commutative operators without the need to define both possible operand combinations? In other words, are there any languages aware of the concept of commutativity itself? And second, was it ever considered in C# and scrapped as it really is just syntactic sugar to avoid a few additional keystrokes that doesn't really add all that much richness to the language (and would most probably need an additional reserved word)?
The commutative property (or commutative law) is a property generally associated with binary operations and functions. If the commutative property holds for a pair of elements under a certain binary operation then the two elements are said to commute under that operation.
If changing the order of the numbers does not change the result in a certain mathematical expression, then the operation is commutative. Only addition and multiplication are commutative, while subtraction and division are noncommutative.
The commutative property deals with the arithmetic operations of addition and multiplication. It means that changing the order or position of two numbers while adding or multiplying them does not change the end result. For example, 4 + 5 gives 9, and 5 + 4 also gives 9.
A set has the commutative property under a particular operation if the result of the operation is the same, even if you switch the order of the elements that are being acted on by the operation.
Are there any languages out there where you can define commutative operators without the need to define both possible operand combinations?
I'm not aware of any, but that doesn't mean they don't exist.
Was it ever considered in C# and scrapped as it really is just syntactic sugar to avoid a few additional keystrokes that doesn't really add all that much richness to the language (and would most probably need an additional reserved word)?
To my knowledge it was never considered. It certainly was never on the list of possible language enhancements, and I don't recall seeing it in the design notes archive.
You make a sufficient argument against the feature in your question. Were I asked to provide an additional critique of the proposed feature I would point out that many overloaded operators are not commutative even when their mathematical counterparts are commutative. String "addition", for example, clearly is not commutative even though both operands are strings. And the same goes for delegate "addition". And there are lots of other properties of operators that would be equally or more useful that I'd want to put in before auto-commutivity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With