class opBinary overloading. value + null and null + value

cal callumenator at gmail.com
Sun Oct 21 13:42:07 PDT 2012


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


More information about the Digitalmars-d-learn mailing list