www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initializing assoc array to non-null

Greetings

Is there a way to initialize an associative array to a non-null (but still
empty) state? The only way I know is by adding an element and then removing
it. Did I miss something obvious? Basically I want to write lines 7-8 in
the following code in a cleaner fashion. Any ideas?

Regards
- Puneet

class Foo {                     // 1
  string[int] _aa;              // 2
  public string[int] aa() {     // 3
    return _aa;                 // 4
  }                             // 5
  this() {                      // 6
    _aa[0] = "";                // 7
    _aa.remove(0);              // 8
  }                             // 9
}                               // 10
void main() {                   // 11
  Foo foo = new Foo;            // 12
  string[int] bb = foo.aa;      // 13
  assert(bb !is null);          // 14
  bb[0] = "zero";               // 15
  assert(bb is foo.aa);         // 16
}                               // 17
Jun 01 2013