One would assume that, given an array literal of various implementations of an interface, the compiler would reduce the literal to an array of that interface. However, the following fails:
interface Inter {
}
class FooImpl : Inter {
}
class BarImpl : Inter {
}
void main()
{
Inter[] test = [new FooImpl(), new BarImpl()];
}
with
poly.d(12): Error: cannot implicitly convert expression (new FooImpl) of type object.Object to poly.BarImpl
poly.d(12): Error: cannot implicitly convert expression ([(__error), new BarImpl]) of type BarImpl[] to Inter[]
Comment #1 by yebblies — 2014-03-02T00:25:55Z
This has the same cause (and fix) as issue 5498.
Comment #2 by mkline.on.d — 2014-03-04T10:04:47Z
*** This issue has been marked as a duplicate of issue 5498 ***
Comment #3 by schveiguy — 2017-07-13T12:15:57Z
Turns out it's not the same.
In the latest DMD (2.074.1):
version(bad) interface A {}
else class A {}
class B : A {}
class C : A {}
void main()
{
A[] arr = [new B, new C];
}
when compiled with version=bad:
test12283.d(9): Error: cannot implicitly convert expression ([new B, new C]) of type Object[] to A[]
Without the version, it works.
My guess is the inference of the type only takes into account the expression, not the fact that it's an initializer. However this is surprising behavior, because in other cases, an initializer does use the type of the variable being initialized to work:
auto x = [1, 2]; // int[]
short[] y = x; // Error
short[] z = [1, 2]; // But this is OK
The only way to get it to work is to cast one element to an A. An ugly error-prone mechanism (which also happens to do a dynamic cast in some cases).
I think this should work if the variable being declared has a type and isn't auto. If it's auto, then the behavior of making an Object[] is a valid answer.
Comment #4 by robert.schadek — 2024-12-13T18:17:38Z