Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commutativity in operators

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)?

like image 924
InBetween Avatar asked Jun 04 '13 14:06

InBetween


People also ask

What is commutative property of an operator?

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.

Which basic operations has Commutativity?

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.

What is an example of a commutative?

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.

How do you know if an operation is commutative?

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.


1 Answers

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.

like image 60
Eric Lippert Avatar answered Oct 31 '22 23:10

Eric Lippert