primitive type operator overload

bearophile bearophileHUGS at lycos.com
Fri Apr 20 09:41:55 PDT 2012


Dominic Jones:

> I want to overload a primitive type operator so that I can do 
> something like
>
> double a;
> myStruct b;
> writeln(a + b);

You can't overload the operator of a primitive, but binary 
operators come in left and right versions:
http://dlang.org/operatoroverloading.html#Binary

a.opBinary!("op")(b)
b.opBinaryRight!("op")(a)

so adding an inverse "+" operator inside myStruct is possible.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list