Bug in usage of associative array: dynamic array with string as a key

Cecil Ward cecil at cecilward.com
Fri Jun 30 19:05:23 UTC 2023


I have code roughly like the following:

    dstring str = "name"d;
    uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
Decls.ordinals[ str ]  :  -1;

struct Decls
    {
    uint[ dstring]   ordinals;
    }

//and
    Decls.ordinals[ str ] = ordinal_counter++;

The problem is that it always returns ordinal== -1 from the 
expression. Can you sort me out? I took this from the example 
given in the language reference under arrays, testing for 
membership (or similar, I forget the subssection title).

 From good old printfs it seems to be the case that the array is 
being populated (elsewhere) with the expected correct values. 
Taking out the if doesn’t seem to help either. I don’t have a way 
of examining the contents of the dynamic array directly to check 
that they are actually being stored as expected, other than 
seeing that that line of code is indeed being executed with the 
expected values of str going in. Note that I’m using 32-bit 
dstrings everywhere, not strings of bytes.



More information about the Digitalmars-d-learn mailing list