Problem with type-casting to interface arrays

Vladimir Panteleev thecybershadow at gmail.com
Sun Mar 18 22:52:15 PDT 2007


Hi,

With an interface I and an implementation C : I, is this code sane?

     C[] c = [new C()];
     I[] i = cast(I[]) c;

Currently, any attempts to do this typecast fail for me (the objects inside i are corrupted/unusable).
I don't see why it should fail (per the spec), so I'm inclined to believe it's a compiler bug.

I've attached three test cases (simple Phobos, Phobos, Tango).

EDIT: Someone has explained to me why does this happen, and that I simply cannot do this because that's how things work. Interface Vtable offsets and such - it would have to create a copy to do this (adjust pointers and such). Still, I think it's just too easy to fall for this - and it's hard to figure out a cause for such a bug. The compiler should at least return null for such a typecast - and, in the best case, display a warning or error.

-- 
Best regards,
  Vladimir                          mailto:thecybershadow at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arraytest_basic.d
Type: text/x-dsrc
Size: 271 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070319/a7d482d5/attachment.d>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arraytest_tango.d
Type: text/x-dsrc
Size: 916 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070319/a7d482d5/attachment-0001.d>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arraytest_phobos.d
Type: text/x-dsrc
Size: 846 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070319/a7d482d5/attachment-0002.d>


More information about the Digitalmars-d mailing list