OpEquals and Interfaces

Ali Çehreli acehreli at yahoo.com
Tue Apr 13 15:04:04 PDT 2010


Christoph Mueller is asking the exact problem that I've been having. :)

Steven Schveighoffer wrote:

 > If you are using D2, there is a workaround:
 >
 > interface I
 > {
 >    final bool opEquals(I other)
 >    {
 >       Object me = cast(Object)this;
 >       Object they = cast(Object)other;
 >       return equals(me, they);

Is 'equals' a function on this interface?

 >    }
 > }

I could not make that work. The compiler is still trying to call 
Object.opEquals.

At the risk of turning this forum to D.learn, I tried to have an 
explicit 'equals' member function on the interface and use that.

This is what I have:

import object;

interface I
{
     final bool opEquals(I other)
     {
         return equals(cast(Object)other);
     }

     bool equals(Object o);
}

class C : I
{
     override bool opEquals(Object o) const
     {
         return true;
     }

     bool equals(Object o)
     {
         return this == o;
     }
}

void main()
{
     I i = new C;
     i == i;         // <-- Error line
}

But it still calls Object.opEquals:

Error: function object.opEquals (Object lhs, Object rhs) is not callable 
using argument types (I,I)
Error: cannot implicitly convert expression (i) of type deneme.I to 
object.Object
Error: cannot implicitly convert expression (i) of type deneme.I to 
object.Object

Ali



More information about the Digitalmars-d mailing list