array of elements of various sybtypes

spir denis.spir at
Mon Jan 31 13:07:13 PST 2011

On 01/31/2011 09:06 PM, Steven Schveighoffer wrote:
> On Fri, 28 Jan 2011 16:59:20 -0500, Don <nospam at> wrote:
>> 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.
> I don't think it should do that. But I'm not sure.
> It seems it can have several choices as to what base to use. For example, what
> if both T1 and T2 implement the X interface, wouldn't X be just as good a choice?
> I'm really unsure what the right thing to do is. But regardless of all this, I
> think there should absolutely be a way to say "I absolutely need this array
> literal to be typed as a U[]" and let the compiler fail if it's not possible.
> -Steve


vita es estrany

More information about the Digitalmars-d-learn mailing list