Associative array trouble

2009-10-16 Thread Justin Johansson
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

Re: Associative array trouble

2009-10-16 Thread Bill Baxter
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

Re: Associative array trouble

2009-10-16 Thread Justin Johansson
Bill Baxter Wrote: You query presence of a key in an AA using 'in' Thank you Bill .. esp. the tip to avoid double lookup. JJ

Re: Associative array trouble

2009-10-16 Thread Bill Baxter
On Fri, Oct 16, 2009 at 4:11 PM, Manfred_Nowak svv1...@hotmail.com wrote: Bill Baxter wrote:   Symbol* pSym = id in symtab; shouldn't the compiler sort this out? I'm not really sure what you mean, but I think the answer is that there's a difference between an unset entry and one that's set

Re: Associative array trouble

2009-10-16 Thread bearophile
Bill Baxter: Or this avoids a double lookup if the symbol is present: In LDC both forms (and several other ones, if the usage of the pointer is local, and it doesn't get passed away) get simplified to the same single lookup code :-) (but not in DMD). So much that you may even think of in to