View mode: basic / threaded / horizontal-split · Log in · Help
February 13, 2007
Re: Object and interface compatibility
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.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home