OpEquals and Interfaces

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 13 13:41:28 PDT 2010


On Tue, 13 Apr 2010 15:50:36 -0400, Christoph Mueller  
<ruunhb at googlemail.com> wrote:

> I'm currently writing a library in D2 which uses intensively interfaces  
> and i meet a problem by overloading the opEquals Operator.
>
> In some of my implementations i want to compare an object through an  
> interface of another instance
>
> Unfortanetly, the opEquals Operator uses only Object parameters and  
> according to the current DMD-Compiler it's not possible to cast implicit  
> an Interface to an Object. (Got a nice compiler error)
>
> Is there any reason to forbid implicit downcasting from any interface to  
> Object?

Any good reason? No.

But the stated reason is usually that interfaces don't necessarily have to  
be Objects, they can be COM objects, which 1) has no bearing in some OSes,  
and 2) does anyone use this feature?

> Of course, explicit downcasting cast(Object) is also possible, but  
> casting everytime for == operator is not really nice.

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);
    }
}

But it would be nice if the compiler did this automatically.  There are  
other things that suck because interfaces are not assumed to be derived  
 from Object.

I think the whole COM interface hack needs to be revisited.

-Steve



More information about the Digitalmars-d mailing list