www.digitalmars.com         C & C++   DMDScript  

D - Syntax Q: How to define static initializers for dynamic & associative

reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
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
parent Burton Radons <loth users.sourceforge.net> writes:
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