Associative array trouble
Bill Baxter
wbaxter at gmail.com
Fri Oct 16 13:52:01 PDT 2009
You query presence of a key in an AA using 'in'
if (id in symtab) {
Symbol sym = symtab[id];
...
} else {
..
}
Or this avoids a double lookup if the symbol is present:
Symbol* pSym = id in symtab;
if (pSym !is null) {
Symbol sym = *pSym;
...
} else {
...
}
--bb
On Fri, Oct 16, 2009 at 1:37 PM, Justin Johansson <no at spam.com> wrote:
> 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