On Saturday, 2 June 2018 at 21:44:39 UTC, greatsam4sure wrote:
Sorry for the typo

is it possible to define infix function in D

3.min(5)// 3: where min is a function, works in D
3 min 5 // does not work.

thanks in advance

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);
}

Reply via email to