Bug 799 – Array literal with covariant types

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-01-06T10:16:00Z
Last change time
2014-02-15T13:13:16Z
Keywords
spec
Assigned to
bugzilla
Creator
aarti

Comments

Comment #0 by aarti — 2007-01-06T10:16:30Z
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.