Associative array trouble

Justin Johansson no at spam.com
Fri Oct 16 13:37:54 PDT 2009


What's wrong with this simple symbol table class?

class Symbol
{
   private char[] id;
   private static Symbol[char[]] symtab;

   private this( string id) {
      this.id = id;
   }

   static Symbol opCall( char[] id) {
      Symbol sym = symtab[id];  // *** ArrayBoundsError here

      if (sym is null) {
         sym = symtab[id] = new Symbol( id);
      }

      return sym;
   }
}


void main() {
  auto sym = Symbol( "foo");
}

Running gives Error: ArrayBoundsError

Does symtab need to be initialized in, say, a static if.
The reference documentation on associative arrays does not suggest that it needs to be.

Thanks for all help.
Justin




More information about the Digitalmars-d-learn mailing list