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

FeepingCreature feepingcreature at gmail.com
Fri Jun 30 19:58:39 UTC 2023


On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote:
> 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?

Impossible to tell without a complete repro, I'm afraid. The 
expression, at least, looks correct at first glance.

Note that you can do `uint ordinal = Decls.ordinals.get(str, 
-1);`.


More information about the Digitalmars-d-learn mailing list