opCmp, opEquals

bearophile bearophileHUGS at lycos.com
Thu Oct 23 12:23:06 PDT 2008

Steven Schveighoffer:

>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 [...]<
>The exception is the default behavior, so it should be what you use for the default case.<

Okay. D works like Java here, it seems. So I'll do the same then, thank you.

>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.<

I see, thank you for the idea.
If you think this is the best solution, then I think that now with the refinment of druntime is the best moment to change things. Later this detail of D2 will be too much frozen to change. So Sean may be asked to express an opinion about this.


Ary Borenszweig:

> int opCmp(Object o) {
>       if (this is o)
>           return 0;
>       assert(typeof(o) is typeof(this));
>       auto oc = cast(typeof(this))o;
>       return this.x - oc.x;
> }

I like something like this better:

int opCmp(Object o) {
      if (this is o)
          return 0;
      auto oc = cast(typeof(this))o;
      assert(oc !is null);
      return this.x - oc.x;

But in the end I'll keep the runtime exception in my opCmp methods.

Thank to everyone that has given me a clue,

More information about the Digitalmars-d-learn mailing list