array cast should be supported corrected

Frank Benoit keinfarbton at googlemail.com
Thu Aug 7 07:56:57 PDT 2008


Frank Benoit schrieb:
> 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.
> 
> 
> 

I filed this as report 2270.



More information about the Digitalmars-d mailing list