Type inference in array literals

klickverbot see at klickverbot.at
Sat Aug 28 07:52:23 PDT 2010


I was really surprised by the fact that the following code does not work 
(DMD 2.048 infers the type of the array literal to B[], not Object[]):

---
class A {}
class B {}

void main() {
    Object[] foo = [ new A, new B ];
}
---

Is this by design? If so, what are the reasons for not using the first 
common supertype of all the elements as type for the array literal?

This is quite irritating, since there apparently is no direct way of 
specifying the array literal save for casting the first element to 
Object (or whatever base class/interface you actually use in your code), 
which looks quite strange.


More information about the Digitalmars-d mailing list