Generic array

Jonathan M Davis jmdavisProg at gmx.com
Wed Nov 16 00:41:09 PST 2011


On Wednesday, November 16, 2011 08:33:04 Dejan Lekic wrote:
> RenatoL wrote:
> > ##[3] arr = [0, "aa", 2.4];
> > 
> > What can i put instead of ##?
> > 
> > In C#, just for example, i can write:
> > 
> > object[] ar1 = new object[3];
> > ar1[0] = 1;
> > ar1[1] = "hello";
> > ar1[2] = 'a';
> > 
> > and it works. But in D
> > 
> > Object[3] arr0 = [0, "aa", 2.4];
> > 
> > and  compiler complains....
> 
> In D, the equivalent would be:
> 
> Variant[] arr = variantArray(0, "aa", 2.4);

Yeah. Only classes can be cast to Object in D. There is no autoboxing or the 
like. There is no common type for everything. Variant is a struct which can 
hold any type thanks to a union, but it's not a common type like object 
essentially is in C#.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list