Dynamic array initialisers

Gide Nwawudu gide at btinternet.com
Thu Apr 17 15:35:41 PDT 2008


On Thu, 17 Apr 2008 18:10:20 -0400, Chris R. Miller
<lordSaurontheGreat at gmail.com> wrote:

>Gide Nwawudu Wrote:
>
>> I have the following code, I am attempting to initialise a variable
>> with a dynamic array. Should the code compile or are lines 6 and 7
>> just wrong? 
>> 
>> test.d
>> ------
>> import std.stdio;
>> 
>> void main() {
>> 	auto  numbers1 = [ 1, 2, 3, 4]; // ok, static array int[4]
>> 	int[] numbers2 = ([ 1, 2, 3, 4])[]; // ok
>> 	auto  numbers3 = [ 1, 2, 3, 4][];   // Doesn't compile
>> 	int[] numbers4 = [ 1, 2, 3, 4][];   // Doesn't compile
>> 	
>> 	writefln(typeof(numbers1).stringof);
>> 	writefln(typeof(numbers2).stringof);
>> }
>> 
>> 
>> C:\>dmd test.d
>> a.d(6): semicolon expected following auto declaration, not '['
>> a.d(7): semicolon expected, not '['
>
>You're not using correct syntax.  The first problem line, auto  numbers3 = [ 1, 2, 3, 4][];, doesn't work because you forgot a comma.  I think you want auto to be evaluated as int[][], however, you have two arrays without a comma seperator.  It should be like this:
>
>auto numbers3 = [ 1, 2, 3, 4], [];
>
>The second line has the same problem, as well as it's improperly declared.  It should be declared as int[][].

I was trying create a dynamic array of ints in the same way that
 "hello"[] creates a dynamic array of chars. Maybe I'm mixing up array
and string syntaxes?

Gide


More information about the Digitalmars-d-learn mailing list