class BaseClass {}
class Class1 : BaseClass {}
class Class2 : BaseClass {}
BaseClass[] array;
void main() {
array=[new BaseClass, new Class1, new Class2];
//Below gives compile time error
//Error: cannot implicitly convert expression (new Class2) of type
//array_literal_with_covariants.Class2 to
//array_literal_with_covariants.Class1
array=[new Class1, new Class2];
}
Comment #1 by fvbommel — 2007-01-06T10:43:18Z
Array literals have type T[], where T is the type of the first element (except when that's a static array, then it's the dynamic version IIRC).
Try this:
array=[cast(BaseClass)new Class1, new Class2];
That should work.
It'd be nice if the original code worked though, so I'm changing this to an enhancement request.
Comment #2 by bugzilla — 2007-02-03T01:05:10Z
The request is to find the "greatest common denominator" to use as the type for an array literal. While this is possible to implement, I feel it is much more understandable to just take the type of the first element.