www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why array do not support gcc style Initialize way?

reply lili <akozhao tencent.com> writes:
Hi:
     In C array init is early than D。
     int ary[3]={[1]=2};
     int ary[100]={[10 ... 20]=10};
     Why D betterC do not support this way。
Sep 11 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 11 September 2019 at 12:00:01 UTC, lili wrote:
 Hi:
     In C array init is early than D。
     int ary[3]={[1]=2};
     int ary[100]={[10 ... 20]=10};
     Why D betterC do not support this way。
D's array initialization uses different syntax from C: int[3] ary = [ 1: 2 ]; // index: value For details, see the D language specification's section on static array initialization: https://dlang.org/spec/arrays.html#static-init-static
Sep 11 2019
next sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 11 September 2019 at 19:19:51 UTC, Paul Backus 
wrote:
 On Wednesday, 11 September 2019 at 12:00:01 UTC, lili wrote:
 Hi:
     In C array init is early than D。
     int ary[3]={[1]=2};
     int ary[100]={[10 ... 20]=10};
     Why D betterC do not support this way。
D's array initialization uses different syntax from C: int[3] ary = [ 1: 2 ]; // index: value For details, see the D language specification's section on static array initialization: https://dlang.org/spec/arrays.html#static-init-static
Wow, I've been using D for 7 years and had no idea this was possible. Normally the [ 1:2 ] syntax defines an associative array; do you happen to know where in the spec this is mentioned, if at all?
Sep 11 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 11 September 2019 at 19:44:47 UTC, Meta wrote:
 On Wednesday, 11 September 2019 at 19:19:51 UTC, Paul Backus 
 wrote:
 D's array initialization uses different syntax from C:

     int[3] ary = [ 1: 2 ]; // index: value

 For details, see the D language specification's section on 
 static array initialization:
 https://dlang.org/spec/arrays.html#static-init-static
Wow, I've been using D for 7 years and had no idea this was possible. Normally the [ 1:2 ] syntax defines an associative array; do you happen to know where in the spec this is mentioned, if at all?
It's right under the header I linked to in my message :)
Sep 11 2019
parent Meta <jared771 gmail.com> writes:
On Wednesday, 11 September 2019 at 19:55:00 UTC, Paul Backus 
wrote:
 On Wednesday, 11 September 2019 at 19:44:47 UTC, Meta wrote:
 On Wednesday, 11 September 2019 at 19:19:51 UTC, Paul Backus 
 wrote:
 D's array initialization uses different syntax from C:

     int[3] ary = [ 1: 2 ]; // index: value

 For details, see the D language specification's section on 
 static array initialization:
 https://dlang.org/spec/arrays.html#static-init-static
Wow, I've been using D for 7 years and had no idea this was possible. Normally the [ 1:2 ] syntax defines an associative array; do you happen to know where in the spec this is mentioned, if at all?
It's right under the header I linked to in my message :)
Whoops, I was too surprised to even read your full post before rushing off to run.dlang.io to test it out for myself.
Sep 11 2019
prev sibling parent reply Roberto Rosmaninho <robertogrosmaninho gmail.com> writes:
On Wednesday, 11 September 2019 at 19:19:51 UTC, Paul Backus 
wrote:
 For details, see the D language specification's section on 
 static array initialization:
 https://dlang.org/spec/arrays.html#static-init-static
Hey, at this section the code below: enum Color { red, blue, green }; int[Color.max + 1] value = [ Color.blue :6, Color.green:2, Color.red :5 ]; Produces the output “onlineapp.d(4): Deprecation: use `{ }` for an empty statement, not `;`” when I executed it. Any clues of why it’s happened?
Sep 11 2019
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Sep 11, 2019 at 08:36:03PM +0000, Roberto Rosmaninho via Digitalmars-d
wrote:
[...]
 enum Color { red, blue, green };
 
 int[Color.max + 1] value =
   [ Color.blue :6,
     Color.green:2,
     Color.red  :5 ];
 
 Produces the output “onlineapp.d(4): Deprecation: use `{ }` for an empty
 statement, not `;`” when I executed it.
 
 Any clues of why it’s happened?
Delete the ';' from the end of your 'enum' declaration. In D, enum declarations do not need to be terminated with ';' (you cannot declare enum variables with it, unlike C). T -- INTEL = Only half of "intelligence".
Sep 11 2019
prev sibling parent ag0aep6g <anonymous example.com> writes:
On 11.09.19 22:36, Roberto Rosmaninho wrote:
 On Wednesday, 11 September 2019 at 19:19:51 UTC, Paul Backus wrote:
[...]
 https://dlang.org/spec/arrays.html#static-init-static
Hey, at this section the code below: enum Color { red, blue, green };
[...]
 Produces the output “onlineapp.d(4): Deprecation: use `{ }` for an empty 
 statement, not `;`” when I executed it.
 
 Any clues of why it’s happened?
The semicolon after the enum is wrong. PR to fix it: https://github.com/dlang/dlang.org/pull/2701
Sep 11 2019