D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17418 - opCmp return value unclear
Summary: opCmp return value unclear
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P1 minor
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2017-05-22 13:42 UTC by Mathias Baumann
Modified: 2022-07-15 10:24 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mathias Baumann 2017-05-22 13:42:40 UTC
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 Jonathan M Davis 2017-05-22 17:47:34 UTC
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 berni44 2019-12-20 18:13:21 UTC
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 RazvanN 2022-07-15 10:24:04 UTC
This has been added to spec.