Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 08, 2004 [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
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; } |
April 08, 2004 Re: [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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 |
April 08, 2004 Re: [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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
|
April 08, 2004 Re: [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "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 |
April 08, 2004 Re: [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Phill |
>
> 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
|
April 08, 2004 Re: [BUG] casting from Object to interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "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 > > Nope I meant that you can go straight from 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 |
Copyright © 1999-2021 by the D Language Foundation