how to define infix function

greatsam4sure greatsam4sure at gmail.com
Sun Jun 3 11:01:30 UTC 2018


On Saturday, 2 June 2018 at 22:09:49 UTC, Neia Neutuladh wrote:
> On Saturday, 2 June 2018 at 21:44:39 UTC, greatsam4sure wrote:
>> [...]
>
> This is a horrible abuse of D's operator overloading discovered 
> by FeepingCreature in the distant past.
>
> You have to delimit your custom infix operator with slashes; 
> you can't make `3 min 5` work, but you can make `3 /min/ 5` 
> work.
>
> Observe:
>
> struct Min
> {
>     MinIntermediate!T opBinaryRight(string op, T)(T value) if 
> (op == "/")
>     {
>         return MinIntermediate!T(value);
>     }
> }
> struct MinIntermediate(T)
> {
>     T value;
>     T opBinary(string op, T)(T value2) if (op == "/")
>     {
>         if (value < value2) return value;
>         return value2;
>     }
> }
> Min min;
> void main()
> {
>     writeln(1 /min/ 2);
> }

thanks


More information about the Digitalmars-d-learn mailing list