array cast should be supported corrected

BCS ao at pathlink.com
Wed Aug 6 13:24:03 PDT 2008


Reply to Frank,

> interface I{}
> class C : I {}
> C[] carray = getInstance();
> I[] iarray = carray; // compile error
> I[] iarray = cast(I[])carray; // runtime error (1)
> // correct way:
> I[] iarray = new I[carray.length];
> foreach( idx, c; carray ){
> iarray[idx] = c; // implicit cast
> }
> I use a template for doing this, but this looks so ugly. I[] iarray =
> arraycast!(I)(carray);
> 
> I think the D compiler should call a runtime method in (1) to do the
> cast in a loop, instead of doing a simple type change that is not
> working correctly.
> 

this has come up before (IIRC) and the reply from Walter was that because 
the conversion is O(n) it should not be put into a cast that is otherwise 
O(1).

OTOH if the cast(I[])carray is broken, that should be fixed (forbidden).





More information about the Digitalmars-d mailing list