October 03, 2002 Syntax Q: How to define static initializers for dynamic & associative arrays? | ||||
---|---|---|---|---|
| ||||
I have structures that look (sort of) like this: struct Foo { Type1[Type2] map; struct _bar { // stuff } _bar[] bars; } I want to declare a static array of these structs, and initialize it with a static initializer: const Foo[5] dataTable = { 0: { // initializers for dataTable[0] } 1: { // initializers for dataTable[1] } ... } What exactly would be the syntax? |
October 04, 2002 Re: Syntax Q: How to define static initializers for dynamic & associative arrays? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | Russell Lewis wrote:
> I have structures that look (sort of) like this:
>
> struct Foo {
> Type1[Type2] map;
>
> struct _bar {
> // stuff
> } _bar[] bars;
> }
>
> I want to declare a static array of these structs, and initialize it with a static initializer:
>
> const Foo[5] dataTable = {
> 0: {
> // initializers for dataTable[0]
> }
> 1: {
> // initializers for dataTable[1]
> }
> ...
> }
>
> What exactly would be the syntax?
There's no initialiser syntax for associative arrays; for normal arrays it's a comma-separated list in between square brackets. I think it should be deferred to the semantic phase to decide whether this is an associative array initialisation or a normal array; so it would be:
char [] [int] dict = [
0: "bar",
1: "y",
];
This amount is easy, the compiler has to do all the stages up to it for normal arrays and it's mostly just a case for the semantic and the code generator. The problem's mostly that building associative arrays in the compiler is no fun. That's not a very good excuse until you have to do it. :-) It's on my TODO, Walter's too I'm sure.
In this exact case, use a static constructor function, and make the array non-const of course.
|
Copyright © 1999-2021 by the D Language Foundation