Overloading relational operators separately; thoughts?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 30 09:25:45 PDT 2016

On Friday, September 30, 2016 12:31:45 Minty Fresh via Digitalmars-d wrote:
> It's up to the developer to ensure that their code works
> correctly with existing facilities, not the language. I can
> already write code that will work in amazing and ridiculous ways
> with generic code in the standard library. It does not follow
> that the language is responsible or at fault for the code
> behaving in ways that are unexpected.

Of course, you can write code that behaves badly. There's no way that that
can be completely prevented while still allowing you to do useful,
productive things. But the folks asking to be able to overload < separately
just so that they can make it behave differently than what opCmp does are
basically asking for a way to be allowed to make their code behave badly
with the comparison operators in a way that opCmp doesn't currently allow.
The request is to improve support for an idiom that many consider to be
horrible coding practice, and Walter does not think that explicitly
supporting such an idiom makes any sense.

He hasn't gone to extra effort to prevent every possible misuse of
overloaded operators that he could. He simply defined some of the overloaded
operators in a way that was a significant improvement for the use case that
he thought that overloaded operators should be used for, and as a side
effect, it happens to have made it harder for a use case that he thinks is
bad practice. I think that he made the right decision.

But if you or anyone else wants to do wacky stuff with overloaded operators
that is not consistent with the built-in types, you're free to do so within
how D's overloaded operators work. You're just not going to be able to do it
as freely as you can in C++, because that goes against the goals of the
operator overloading feature in D, and some of the improvements that were in
line with the goals happen to have gotten in the way of such operator
overloading abuse.

- Jonathan M Davis

More information about the Digitalmars-d mailing list