opCmp, opEquals
Steven Schveighoffer
schveiguy at yahoo.com
Thu Oct 23 12:04:21 PDT 2008
"bearophile" wrote
> Steven Schveighoffer:
>> Given that the default implementation just compares references, I think
>> C1
>> is correct.
>> Otherwise you have weird shit like this:
>> [...something bad]
>
> In that C1 class I have left out the implementation of opCmp, let's say I
> use the following one (C4a and C4b have the same code, but they are
> distinct classes):
> [snip]
> void main() {
> auto x = new C4a(1);
> auto y = new C4b(2);
>
> printf(x == y ? "x == y\n" : "x != y\n"); // else
> printf(x.opCmp(y) == 0 ? "x ==2 y\n" : "x !=2 y\n"); // throws
> }
>
> I think that's more bad behavior.
Yes, but consistently bad ;)
that is, x.opCmp(y) results in the same thing (throwing an exception) as
someObj.opCmp(y), where someObj's opCmp is the default implementation (note
that this is currently broken in Tango, but not in druntime).
The real solution here is to have opCmp not be in object, but in an
interface. The fact that the default opCmp throws an exception is a huge
clue to this.
But I doubt that's changing anytime soon.
> But then what can I do? remove the UncomparableException from opCmp too,
> and make it return randomly -1 or +1 when the classes can't be cast, like
> this?
The exception is the default behavior, so it should be what you use for the
default case.
-Steve
More information about the Digitalmars-d-learn
mailing list