array of elements of various sybtypes

Don nospam at nospam.com
Fri Jan 28 13:59:20 PST 2011


spir wrote:
> Hello,
> 
> This fails:
> 
> class T0 {}
> class T1 : T0 {}
> class T2 : T0 {}
> 
> unittest {
>     auto t1 = new T1();
>     auto t2 = new T2();
>     T0[] ts = [t1, t2];
> }
> 
> Error: cannot implicitly convert expression (t1) of type __trials__.T0 
> to __trials__.T2
> Error: cannot implicitly convert expression ([(__error),t2]) of type 
> T2[] to T0[]
> 
> I guess it should be accepted due to explicite typing 'T0[]'. What do 
> you think? D first determines the type of the last element (always the 
> last one), here T2. Then, /ignoring/ the array's defined type, tries to 
> cast other elements to the same type T2. 

No, it's not supposed to do that. What is supposed to happen is, to use 
?: to determine the common type of the array. This will give a T0[] array.
Then, it attempts to implicitly cast to the defined type.

Your code should work. You've just hit an implementation bug.


More information about the Digitalmars-d-learn mailing list