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

Nicholas Wilson iamthewilsonator at hotmail.com
Wed Feb 13 01:04:15 UTC 2019


On Wednesday, 13 February 2019 at 00:24:48 UTC, Jonathan M Davis 
wrote:
> Except that what you're proposing is precisely so that you can 
> more easily abuse

Use, not abuse.

> certain operators when overloading them. IMHO, if an operator 
> is being overloaded in a manner that doesn't fit what you get 
> with the basic arithmetic types, then that's bad practice.

Yes, thats called abuse.

> People kept using [C++ operators] to mean things that didn't 
> match their meaning for the built-in types. Such operator 
> overloading abuse results in code that's harder to understand 
> and maintain, because the operators are not being used with 
> their normal meaning.

That falls down to convention. I personally think << for I/O is 
silly, but the thing that makes it work is that it is 
consistently applied.

> It also doesn't play well with generic code, because such code 
> is going to assume the normal arithmetic meanings for those 
> operators, and so having a type that uses them differently with 
> such code will tend to result in incorrect behavior (or in some 
> cases, the code won't even compile).

Actually it plays excellently with generic code. If I have a 
symbolic execution engine and I use it with a generic function, 
what results is a symbolic function, for free! You are then free 
to do what ever you please with, e.g. turn it into a DB query, 
send it off to a graph execution engine, optimise it, evaluate it 
at a later time, ... .

> We can't completely eliminate operator overloading abuse (e.g. 
> someone doing something dumb like making + mean -), but just 
> because it's possible to abuse one operator doesn't mean that 
> we should make easier to abuse another.

It doesn't mean we shouldn't either, it bears no correlation. 
What we want to prevent is abuse, not potential for abuse. We 
already have massive potential for abuse, goto, operator 
overloading, mixins, etc., we have almost no _actual_ abuse. D is 
designed for someone who is sensible and knows how to use 
discretion.

Yes this proposal increases the potential for abuse. How much of 
that potential will actually materialise? History suggests not 
much, and that should be reflected in the decision making.



More information about the Digitalmars-d mailing list