ProtoObject and comparison for equality and ordering
Mike Franklin
slavo5150 at yahoo.com
Thu May 16 06:20:57 UTC 2019
On Wednesday, 15 May 2019 at 16:36:27 UTC, H. S. Teoh wrote:
> FYI, even with the above amusingly elaborate hack, you still
> cannot achieve proper 4-way comparison results. Consider: what
> should OverengineeredCmpResult.opCmp return for payload ==
> ionno, such that <, <=, >, >=, ==, != would all produce the
> correct result?
>
> Answer: it's not possible unless you return float, because x <
> y translates to x.opCmp(y) < 0, and x > y translates to
> x.opCmp(y) > 0, so the only way to represent an incomparable
> state is for opCmp to return some value z for which z < 0 and z
> > 0 are *both* false. There is no integer value that fits this
> description; the only candidate is float.nan. Substituting the
> return value of opCmp with a custom struct doesn't fix this
> problem; it only defers it to the custom struct's opCmp, which
> suffers from the same problem.
I see. I'm a little embarrassed I didn't realize that but happy
you took the time to explain it. Thank you.
Mike
More information about the Digitalmars-d
mailing list