On 2012-10-10, 14:24, Marco Leise wrote:

enum Comparison {
     Before = -1,
     Same = 0,
     After = 1,
     Unordered = NaN,
}

a) There is no integer NaN and floating point is a no-go.

The NaN was intended as a joke, and to highlight the fact that
you can have opCmp return a float today, and get some of the
behavior that floats have.


b) If you mix sortable and unsortable most algorithms fail.

Otherwise an enum solution is good, but a bit longer in
code:

auto rel = a.OpCmp(b);
if (rel < 0) ...;
else if (rel > 0) ...;
else ...;

vs.

if (rel == Comparison.Before) ...;
else if (rel == Comparison.After) ...;
else ...;

And in many cases right now you can just "return a - b";

Yeah, this last part is what I really like about the
current solution. Of course, if we could have ranged enums:

enum Comparison {
    Before < 0,
    Same = 0,
    After > 0
}

But that's not going to happen (unless someone provides it
in a library, of course).

--
Simen

Reply via email to