Multidimensional array

bearophile bearophileHUGS at lycos.com
Thu Jul 4 18:43:44 PDT 2013


Ali Çehreli:

> However, that is a confusing syntax because the right-hand side 
> is not the same type as the elements, which is dchar[3]. 
> Perhaps D supports it for C compatibility?
>
> It doesn't match the following. Here, the right-hand side is 
> the same as the element type:
>
>     int[2] arr2 = 42;
>     assert(arr2 == [ 42, 42 ]);
>
> But this doesn't compile:
>
>     char[3][5] arr = [ '.', '.', '.' ];
>
> Error: mismatched array lengths, 15 and 3
>
> I see that as a bug but can't be sure.

In D char literals as 'x' or even string literals as "xx" are 
seen as instances of all the types of strings and chars, it's not 
a bug, it's a feature:

void main() {
     char x1  = 'x';
     wchar x2 = 'x';
     dchar x3 = 'x';
     string s1  = "xx";
     wstring s2 = "xx";
     dstring s3 = "xx";
}


There is a way to specify the type of a string, so this gives 
errors:

void main() {
     string s1  = "xx"d;
     string s2  = "xx"w;
     wstring s3 = "xx"d;
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list