array of elements of various sybtypes
spir
denis.spir at gmail.com
Wed Jan 26 15:42:29 PST 2011
On 01/26/2011 07:29 PM, Jesse Phillips 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[]
>
> D takes the type of the last element. I guess it has been decided to take the common type but hasn't been implemented. Anyway your two reasonable options are:
>
> auto ts = to!(T0[])([...]);
>
> auto ts = [new T1(), to!(T0)(new T2())]
>
> I recommend using std.conv.to instead of a cast because it is safer. Though cast is a no-op and isn't any less safe in this case.
Right, casting a /single/ element works:
auto x = [cast(T0)(t1), t2];
auto y = [t1, cast(T0)(t2)];
But to! fails:
auto x = [to!(T0)(t1), t2];
auto y = [t1, to!(T0)(t2)];
/usr/include/d/dmd/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S)
if (!implicitlyConverts!(S,T) && isSomeString!(T) && isInputRange!(Unqual!(S))
&& isSomeChar!(ElementType!(S))) toImpl(T,S) if (!implicitlyConverts!(S,T) &&
isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S)))
matches more than one template declaration,
/usr/include/d/dmd/phobos/std/conv.d(559):toImpl(Target,Source) if
(implicitlyConverts!(Source,Target)) and
/usr/include/d/dmd/phobos/std/conv.d(626):toImpl(T,S) if (is(S : Object) &&
is(T : Object))
Already had this endless error message once ;-)
Looks like another bug of non mutually exclusive template constraints?
Denis
--
_________________
vita es estrany
spir.wikidot.com
More information about the Digitalmars-d-learn
mailing list