February 13, 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 don't think it's a failure. Like others have said, the interface may or may not be part of a D Object. You can cast to make sure:

Object o = cast(Object)iface;
if (o) {
  // it's a D object; o.toHash is available
}
else {
  // not a D object; panic
}

L.
1 2
Next ›   Last »