class opBinary overloading. value + null and null + value
ref2401
refactor24 at gmail.com
Sun Oct 21 13:54:12 PDT 2012
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
More information about the Digitalmars-d-learn
mailing list