Bug 6028 – Cannot implicitly convert an array of class to an array of interface
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Mac OS X
Creation time
2011-05-18T09:12:00Z
Last change time
2011-05-18T10:13:59Z
Keywords
rejects-valid
Assigned to
nobody
Creator
kennytm
Comments
Comment #0 by kennytm — 2011-05-18T09:12:23Z
Test case:
----------------------------
interface X {}
class K : X {}
class L : K {}
void main() {
L[] x;
K[] y = x; // ok
X[] z = y; // error
}
----------------------------
test.d(8): Error: cannot implicitly convert expression (y) of type K[] to X[]
----------------------------
It should be possible to implicitly convert an array of class to an array of (one of) its base interface type.
Comment #1 by schveiguy — 2011-05-18T10:13:59Z
Actually, you shouldn't be able to convert either.
See issue 2095
Though converting from a class array to an interface array is even worse -- an interface reference is not the same address of a class reference. So this implicit cast would not only have to rewrite all the elements, it would corrupt the original array.