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

Rubn where at is.this
Wed Feb 13 21:01:46 UTC 2019


On Wednesday, 13 February 2019 at 02:00:32 UTC, Nicholas Wilson 
wrote:
> On Wednesday, 13 February 2019 at 01:24:45 UTC, Rubn 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
>
> Oooh. That one's nasty! Technically unambiguous, but nasty!
>
>> 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.
>
> With opBinary doing something completely different to 
> opBinaryRight? Anyway that falls under the category of 
> deliberate abuse, something we should not be considering, if 
> people do that kind of thing then they should get what they 
> deserve.

There are valid deliberate uses for this. For scalar types 
multiplication might be commutative, but for matrix 
multiplication it is not commutative. What good would it be to 
not be able to use an operator that denotes multiplication in the 
way it is intended to be used regarding the two types that are 
being multiplied. If you have a poorly named function it is in 
effect the same thing as misusing an operator. It'd be the same 
as misnaming a function, you read the function it doesn't do what 
the name suggests you end up having to look it up and you know 
for next time. Arguably it'd be easier to detect, if you say a 
class named "Magic" and it is multiplied by an integer, it makes 
you wonder what it is doing. But if you have have a function in 
"Magic" called "deleteUniverse()" what do you think that function 
is going to do? Oh when you look inside you find an empty 
function with a comment "TODO: figure out if this is possible". 
Which is worse in this situation? I'd argue the misnamed function 
is much more dangerous, at least if I see an operator being used 
with a custom type I know I should lookup what the operator 
means. There's also no good way to prevent poorly named functions.



More information about the Digitalmars-d mailing list