Object and interface compatibility

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Feb 10 10:36:25 PST 2007


Frank Benoit (keinfarbton) wrote:
> Interfaces are NOT compatible to Object.
> 
> interface I{
>  void func();
> }
> class C : I {
>  void func(){}
> }
> 
> void main(){
>  I i = new C;
>  i.func();                 // OK
>  i.toHash();               // error !!
>  (cast(C)i).toHash();      // OK
>  (cast(Object)i).toHash(); // OK
> }
> 
> I think this is a failure in the language design. What do you think?

I think it isn't a failure unless it prevents some expected core functionality. 
Interfaces are a contract declaring specific small set of members that should be 
available, and make zero guarantees about what else the Object may have going on.

Unfortunately in the case of D, it does prevent something: the use of Interfaces as AA 
keys.  Although, perhaps the thing to do is to have a "standard" IComparable interface 
that declares opCmp, opEquals, and toHash.  Or, have a "root interface" named... 
Interface, I suppose, which declares the same.  (We could even then redeclare Object as 
Object:Interface.)  Then have all user-defined interfaces without explicit inheritance 
derive from Interface the same way classes do from Object.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list