Bug 12283 – Array literals of interface implementations do not resolve to interface

Status
REOPENED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-01T15:16:12Z
Last change time
2024-12-13T18:17:38Z
Assigned to
No Owner
Creator
Matt Kline
See also
https://issues.dlang.org/show_bug.cgi?id=24178
Moved to GitHub: dmd#18784 →

Comments

Comment #0 by mkline.on.d — 2014-03-01T15:16:12Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18784 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB