Bug 948 – operatoroverloading.html - Rationale section is both out of date and incomplete
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-02-11T18:00:00Z
Last change time
2014-02-16T15:24:06Z
Assigned to
bugzilla
Creator
smjg
Comments
Comment #0 by smjg — 2007-02-11T18:00:52Z
At the end of the "Overloading <, <=, > and >=" section is a "Rationale", which states:
"For some objects, testing for less or greater makes no sense. For these, override opCmp() with: " [...]
There are two things wrong with this (besides the fact that that code would now trigger a "statement is not reachable" warning in DMD, but that's irrelevant):
- The instruction to override opCmp is a leftover from when the default Object.opCmp compared the memory address. Nowadays, Object.opCmp already throws an error, so there's no point overriding a class's opCmp as an extra.
- This has been briefly mentioned before but nothing done about it. On reading just above it that "every class object has a opCmp()", people will look for an explanation of _why_ opCmp should be defined for every class. Instead, the average reader is treated to what reads as an argument _against_ this, namely "For some objects, testing for less or greater makes no sense."
I propose the following rewrite of the bullet points in this section to address both problems:
- Testing for equality can sometimes be a much more efficient operation than testing for less or greater than.
- Having opCmp defined in Object makes it easier to implement associative arrays and sorting efficiently.
- For some classes, testing for less or greater makes no sense. The default Object.opCmp throws an error at runtime to indicate that the programmer has not defined an opCmp for the class being used.