Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
April 17, 2008 Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
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 '[' Gide |
April 17, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gide Nwawudu | 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[][].
|
April 17, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris R. Miller | On Thu, 17 Apr 2008 18:10:20 -0400, Chris R. Miller <lordSaurontheGreat@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
|
April 18, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gide Nwawudu | Gide Nwawudu wrote:
> On Thu, 17 Apr 2008 18:10:20 -0400, Chris R. Miller
> <lordSaurontheGreat@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
It's implicitly castable to a dynamic array (I think). Just don't use auto (isn't that "feature" annoying?):
int[] numbers = [1, 2, 3, 4];
|
April 18, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris R. Miller | "Chris R. Miller" <lordSaurontheGreat@gmail.com> wrote in message news:fu8hsc$71p$1@digitalmars.com... > auto numbers3 = [ 1, 2, 3, 4], []; Sorry, that's entirely wrong. First, he wants an int[]. Second, this is just using the comma operator. It would assign [] into numbers3 (if it compiled, which I don't know). |
April 18, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Fraser | On Thu, 17 Apr 2008 17:33:45 -0700, Robert Fraser <fraserofthenight@gmail.com> wrote:
>Gide Nwawudu wrote:
>> On Thu, 17 Apr 2008 18:10:20 -0400, Chris R. Miller <lordSaurontheGreat@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
>
>It's implicitly castable to a dynamic array (I think). Just don't use auto (isn't that "feature" annoying?):
>
>int[] numbers = [1, 2, 3, 4];
Thanks, maybe I've been (ab)using auto too much.
Gide
|
April 18, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gide Nwawudu | Gide Nwawudu wrote:
> Thanks, maybe I've been (ab)using auto too much.
>
> Gide
No you haven't; that's a perfectly clear use of auto. It's a language issue that needs to be addressed.
|
April 21, 2008 Re: Dynamic array initialisers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Fraser | On Fri, 18 Apr 2008 15:40:48 -0700, Robert Fraser <fraserofthenight@gmail.com> wrote: >Gide Nwawudu wrote: >> Thanks, maybe I've been (ab)using auto too much. >> >> Gide > >No you haven't; that's a perfectly clear use of auto. It's a language issue that needs to be addressed. I've added it as a bug. http://d.puremagic.com/issues/show_bug.cgi?id=2017 |
Copyright © 1999-2021 by the D Language Foundation