Overloading relational operators separately; thoughts?

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 28 23:48:00 PDT 2016


On 2016-09-28 06:02, Walter Bright wrote:

> The limitations are deliberate based on the idea that comparison
> operators need to be consistent and predictable, and should have a close
> relationship to the mathematical meaning of the operators. Overloading
> <= to mean something other than "less than or equal" is considered poor
> style in D, and the same goes for the other arithmetic operators.

If that is not allowed, why is this allowed:

struct MyInt
{
     int value;

     MyInt opBinary(string op)(MyInt rhs) if (op == "+")
     {
         return MyInt(value - rhs.value);
     }
}

assert(MyInt(3) + MyInt(3) == MyInt(0));

The language you just provide a set of tools, then it's up the to the 
programmer to do what he/she wants to do.

> The use of them to create DSLs (a technique called "expression
> templates" in C++) is discouraged in D, for several reasons. The
> recommended way to create DSLs in D is to parse strings using CTFE.

That's one of the ugliest things about D. Because suddenly you will not 
have any help of any tools, like editors, IDEs, linters, syntax analysis 
and so on. One also needs to implement a complete parser, how many 
complete parsers do we have for D?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list