On Sunday, 21 October 2012 at 20:42:08 UTC, cal wrote:
On Sunday, 21 October 2012 at 20:34:51 UTC, ref2401 wrote:
what should i do to make my binary operation commutative?

You can overload opBinaryRight:

MyClass opBinaryRight(string op: "+")(MyClass rhs)
{
   if (rhs is null)
   {
     return null;
   }
   MyClass result = new MyClass(value + rhs.value);
   return result;
}

http://dlang.org/operatoroverloading.html

in the following case

auto v5 = v1 + v2;

there will be compile time error:
overloads pure @safe MyClass(MyClass rhs) and pure @safe MyClass(MyClass lhs) both match argument list for opBinary

Reply via email to