www.digitalmars.com         C & C++   DMDScript  

D - [BUG] casting from Object to interface

reply Ben Hinkle <bhinkle4 juno.com> writes:
based on Kris's interface experiments here is a simple example
of one of the problems he is talking about. As written below it
will cause an Access Violation. If you either cast from x or
make C extend A instead of B then everything works ok. Something
about dynamic casting from Object to an interface that extends
another interface. I don't know if Kris has already posted an
example like this but I thought I'd go ahead and post anyway.

interface A {}
interface B:A {}
interface C {}
class D:B,C {}
int main()
{
    D x = new D();
    printf("D: %p\n",x);
    Object o = x;
    printf("o: %p\n",o);
    B b = cast(B)o;
    printf("B: %p\n",b);
    C c = cast(C)o;    // boom
    printf("C: %p\n",c);
    return 0;
}
Apr 07 2004
next sibling parent "Derek Parnell" <Derek.Parnell psyc.ward> writes:
On Wed, 07 Apr 2004 23:00:20 -0400 (08/Apr/04 01:00:20 PM)
, Ben Hinkle <bhinkle4 juno.com> wrote:

 based on Kris's interface experiments here is a simple example
 of one of the problems he is talking about. As written below it
 will cause an Access Violation. If you either cast from x or
 make C extend A instead of B then everything works ok. Something
 about dynamic casting from Object to an interface that extends
 another interface. I don't know if Kris has already posted an
 example like this but I thought I'd go ahead and post anyway.

 interface A {}
 interface B:A {}
 interface C {}
 class D:B,C {}
 int main()
 {
     D x = new D();
     printf("D: %p\n",x);
     Object o = x;
     printf("o: %p\n",o);
     B b = cast(B)o;
     printf("B: %p\n",b);
     C c = cast(C)o;    // boom
     printf("C: %p\n",c);
     return 0;
 }

And if you say "class D:C,B {}" instead of "class D:B,C {}", it works fine. -- Derek
Apr 07 2004
prev sibling next sibling parent John Reimer <jjreimer telus.net> writes:
Ben Hinkle wrote:

 based on Kris's interface experiments here is a simple example
 of one of the problems he is talking about. As written below it
 will cause an Access Violation. If you either cast from x or
 make C extend A instead of B then everything works ok. Something
 about dynamic casting from Object to an interface that extends
 another interface. I don't know if Kris has already posted an
 example like this but I thought I'd go ahead and post anyway.

I read Kris's document on interfaces. It was very well written, and I learned quite a bit from the short essay. Perhaps it could be improved: for each item in the list of 11 interface uses, it would be nice to have a small example demonstrating the manditory interface application that he mentions. Code sometimes explains what's difficult to comprehend in English. (I understand that some features on the list might not lend themselves well to code examples). Later, John
Apr 07 2004
prev sibling parent reply "Phill" <phill pacific.net.au> writes:
"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:4of970hv6g5j93msf30el85ckvs0cg0jtb 4ax.com...
 based on Kris's interface experiments here is a simple example
 of one of the problems he is talking about. As written below it
 will cause an Access Violation. If you either cast from x or
 make C extend A instead of B then everything works ok. Something
 about dynamic casting from Object to an interface that extends
 another interface. I don't know if Kris has already posted an
 example like this but I thought I'd go ahead and post anyway.

 interface A {}
 interface B:A {}
 interface C {}
 class D:B,C {}
 int main()
 {
     D x = new D();
     printf("D: %p\n",x);
     Object o = x;
     printf("o: %p\n",o);
     B b = cast(B)o;
     printf("B: %p\n",b);
     C c = cast(C)o;    // boom
     printf("C: %p\n",c);
     return 0;
 }

In Java you wouldnt even need to cast that. Phill. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.648 / Virus Database: 415 - Release Date: 3/31/2004
Apr 07 2004
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
 In Java you wouldnt even need to cast that.

Umm, do you mean java.lang.Object implicitly casts to anything? It shouldn't. If the static type is a subclass or implements the type you are casting to then the cast can be implicit in Java and D but otherwise you need to perform a run-time cast. I think D is running into trouble because the run-time cast behavior seems to depend on the static type - which is really wierd. -Ben
Apr 08 2004
parent "Phill" <phill pacific.net.au> writes:
"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:c53ja7$11mq$1 digitaldaemon.com...
 In Java you wouldnt even need to cast that.

Umm, do you mean java.lang.Object implicitly casts to anything? It shouldn't. If the static type is a subclass or implements the type you are casting to then the cast can be implicit in Java and D but otherwise you need to perform a run-time cast. I think D is running into trouble because the run-time cast behavior seems to depend on the static type - which is really wierd. -Ben

the class to interface like this: class Test implements Runnable{ public static void main(String[] args) { Test t = new Test(); Runnable r = t; } public void run(){} } As you well know if you wanted to go from Object to interface then you would need to go: Test t = new Test(); Object o = t; Runnable r = (Runnable)o; Phill. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.648 / Virus Database: 415 - Release Date: 3/31/2004
Apr 08 2004