A fresh look at comparisons

Georg Wrede georg at nospam.org
Tue Apr 15 04:26:26 PDT 2008


Janice Caron wrote:
> As you know, in existing D, opEquals(), at least for classes, is
> defined to take an Object parameter, not a C parameter, so even
> ignoring the inheritance rules, allowing opEquals to compare against
> multiple types is tricky. You end up writing code like
> 
>     int opEquals(Object o)
>     {
>         A a = cast(A)o;
>         B b = cast(B)o;
>         C c = cast(C)o;
> 
>         if (c !is null)
>         {
>             ...
>         }
>         else if (b !is null)
>         {
>             ...
>         }
>         else if (a !is null)
>         {
>             ...
>         }
>     }
> 
> which is not ideal

Ok, but bear with me here for a moment. Suppose you need to write 
specific code for comparisons between A, B, and C. You'd have to tell 
the compiler somehow which of your code lines are for which comparison, 
right?

And the above is not very much typing. And it seems flexible and clear 
enough. Now, changing the syntax would add yet another thing for the 
compiler to think about, and, if it doesn't bring at least the same 
clarity plus some significant enhancement to productivity, clarity, ease 
of use, or something else, then what's the use.

So, maybe I'm missing the real point here?



More information about the Digitalmars-d mailing list