templating opEquals/opCmp (e.g. for DSL/expression templates)

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Feb 13 02:37:50 UTC 2019


On Wed, Feb 13, 2019 at 01:24:45AM +0000, Rubn via Digitalmars-d wrote:
[...]
> Always hear that D is somehow better than C++ for operators but it
> isn't in quite a few places already.
> 
>     int a;
>     auto c = (a) <= 0; // ok
>     auto d = (a) => 0; // not ok

Haha... yeah, I *thought* the choice of => for lambda syntax was not a
good idea.  But people seemed to love it at the time, what can I say?
:-/


> For some reason Walter thought in D if you overload the "+" operator
> you couldn't make it not commutative?? Still never got a reply to
> that, so I'll just assume he didn't know what commutative was. Yes you
> can make "a + b != b + a" be true quite easily.

You don't need operator overloading for that; '+' is already
non-commutative with IEEE 754 floating-point. In fact, most of IEEE 754
arithmetic is non-commutative, even though it appears to be in most
cases.


> Then you have things like "min" where you can do:
> 
>     foo( a /min/ b );
> 
> To get the "min" value between a and b. I guess you could use this as
> an example of why not to allow.

Yes, that's a fine example of operator overloading abuse.


> But at the same time, we're already pretty much there. That includes
> "==" operator in that. So the comparisons operators aren't even
> consistent.
[...]

Don't get me wrong, the situation in D isn't perfect, but imperfection
shouldn't be a reason to open the door to worse things.


T

-- 
If you look at a thing nine hundred and ninety-nine times, you are perfectly safe; if you look at it the thousandth time, you are in frightful danger of seeing it for the first time. -- G. K. Chesterton


More information about the Digitalmars-d mailing list