Bug 4030 – Better object array literal type inference
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-30T02:59:00Z
Last change time
2015-06-09T05:13:44Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-03-30T02:59:07Z
In the following D2 program the compiler can refuse the line 6, because it must refuse to guess (if p1 is an array of A or B).
But at line 7 the compiler has enough information to not guess (it's an array of A), so line 7 can be accepted by the compiler:
class A {}
class B : A {}
final class C1 : B {}
final class C2 : B {}
void main() {
auto p1 = [new C1, new C2]; // line 6, don't guess
A[] p2 = [new C1, new C2]; // line 7, no need to guess
}
Comment #1 by code — 2010-08-28T16:23:02Z
There is an even shorter example which fails on DMD 2.048:
---
class A {}
class B {}
void main() {
Object[] foo = [ new A, new B ];
}
---
TDPL has: "The element type of the array is determined by agreement among all elements of the array, which is computed by means of the conditional operator ? : (anticipating ยง 2.3.16)."
Comment #2 by nfxjfg — 2010-08-28T16:42:33Z
*** This issue has been marked as a duplicate of issue 3543 ***