D - Syntax Q: How to define static initializers for dynamic & associative
- Russell Lewis (19/19) Oct 03 2002 I have structures that look (sort of) like this:
- Burton Radons (16/40) Oct 03 2002 There's no initialiser syntax for associative arrays; for normal 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?
Oct 03 2002
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.
Oct 03 2002