Overloading relational operators separately; thoughts?

pineapple via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 28 03:06:43 PDT 2016


On Wednesday, 28 September 2016 at 03:28:50 UTC, Minty Fresh 
wrote:
> Using strings and mixins does have quite a number of downsides. 
> The additional work required to past the DSL aside, you also 
> usually lose the scope in which things are declared (especially 
> if you wish to reference or declare variables/functions/other 
> constructs within your DSL), error reporting becomes much less 
> precise, and the transition between the outer code and the 
> inner DSL becomes jarring (especially in terms of syntax 
> highlighting).
>
> That said, I love the fact D has a separate operator for 
> concatenation. It's extremely useful to not have that ambiguity.
>
> Another thought is, operators not behaving as expected doesn't 
> strike me as a failure of the language, given the language 
> supports operator overloading. It's a failure in documentation 
> to explain what the operators are intended to do, or a failure 
> in implementation to behave as they should.

I agree that opCmp has the benefit of unambiguity, but I still 
think Minty makes very good points.

I think the cleanest possible solution would be to allow 
comparison operators to be overridden with opBinary and 
opBinaryRight, as well, but use opCmp and opEquals instead if 
they exist. I can't think of any way this would break existing 
code.

On Wednesday, 28 September 2016 at 09:48:48 UTC, Matthias Bentrup 
wrote:
> In Mathematics the comparison operators are also commonly used 
> for semi orders, which cannot be implemented by opCmp, because 
> opCmp has no way to indicate that two values are incomparable.
>
> Interestingly the floating point types are semi ordered (due to 
> NaNs), and for those D has some (non-overridable and 
> deprecated) operators like !<, which would be easily extendable 
> to any semi order, whereas the suggested replacement (i.e. test 
> for NaNs manually) works only on floats.

It's probably a terrible idea, but I'd love if those FP 
comparison operators would be de-deprecated and made possible to 
overload using opBinary.

On Wednesday, 28 September 2016 at 06:05:54 UTC, Jonathan M Davis 
wrote:
> The increment and decrement operators are in a similar boat to 
> the comparison operators in that post and pre can be derived 
> from a single function. So, it's not just the comparison 
> operators that got combined.

Incidentally, I'm a little surprised and disappointed that D 
doesn't allow pre- and postfix operators to be separately 
overloaded. Postfix operators should default to their current 
behavior, where `x<op>` is the same as `auto t = x; x = <op>x; 
return t;` where prefix unary <op> is defined, but I also think 
it'd be fantastic if we introduced an opUnaryPostfix override. 
(And should that be done, for the sake of clarity, it might be a 
good idea deprecate usage of opUnary in favor of calling the 
method opUnaryPrefix.)





More information about the Digitalmars-d mailing list