Bug 17418 – opCmp return value unclear

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-05-22T13:42:40Z
Last change time
2022-07-15T10:24:04Z
Keywords
spec
Assigned to
No Owner
Creator
Mathias Baumann

Comments

Comment #0 by mathias.baumann — 2017-05-22T13:42:40Z
When implementing an opCmp, I pretty much always need to look up how it is defined and the specification constantly disappoints but not saying a word of what the function should actually return for which case. you can kind of extract that info from the re-write rules, but why make it so difficult and not just say something like RETURN A B -1 2 1 0 2 2 +1 1 2 (or whatever the correct numbers are) Link to unclear definition: http://dlang.org/spec/operatoroverloading.html#compare
Comment #1 by issues.dlang — 2017-05-22T17:47:34Z
I would point out that the return values don't need to be spcecific numbers. They only need to be < 0, == 0, or > 0. And requiring them to be -1, 0, and +1 would be less efficient in a number of cases. Now, looking over that page, I agree that it's not as clear as it should be, and the problem seems to be that it's focused on how the compiler is going to take the return value and use that to generate the correct code for the comparison rather than what the programmer needs to do with opCmp to make it work correctly. But from the perspective of the person writing opCmp, opCmp should return a value < 0 if lhs < rhs, 0 if lhs == rhs, and a value > 0 if lhs > rhs.
Comment #2 by bugzilla — 2019-12-20T18:13:21Z
Meanwhile the definition [1] defines three cases returning an int (<, ==, >), but as far as I know, it's also possible to have reals as return types and in this cases NaN is a fourth possible result. [1] https://dlang.org/phobos/object.html#.Object.opCmp
Comment #3 by razvan.nitu1305 — 2022-07-15T10:24:04Z
This has been added to spec.