A fresh look at comparisons

Janice Caron caron800 at googlemail.com
Mon Apr 14 04:49:17 PDT 2008


On 14/04/2008, Henning Hasemann <hhasemann at web.de> wrote:
>
>  I like this proposal but it might have efficiency issues when you need
>  to find out quickly if you want to order two values (you would often
>  have to call both functions).


The best way to fix the efficiency issue would be to tell the compiler
whether the comparison was fully ordered or not. If the comparison is
fully ordered, then the compiler may implement

    a <= b

as

    !(b < a)

whereas, if the comparison is /not/ fully ordered, then the compiler
would need to go the long route, and implement it as

    a == b || a < b

So the question then becomes, how we tell the compiler whether or not
a comparison is fully ordered. My suggestion would be, assume yes by
default, and use a slightly modified syntax if not. e.g.

    class NotFullyOrdered
    {
        int x;

        is(this < c, unordered)
        {
            return whatever;
        }
    }

The default, compiler-generated comparison function, if not supplied
by the programmer, should be

        is(this < c, unordered)
        {
            /* recursive memberwise comparison */
        }

That buys you the best of all worlds.



More information about the Digitalmars-d mailing list