Initializing values in associative arrays

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Nov 29 11:16:40 PST 2012


How safe is it to rely on doing something like this:

     uint[size_t] x;

     foreach(i; iota(0, 10))
         x[i]++;

     foreach(i; iota(5, 15))
         x[i]++;

... ?  In this case "correct" output comes out -- that is, values for keys 1-4 
and 10-14 come out as 1, values for keys 5-9 come out as 2 -- but it seems a bit 
shaky to assume that just incrementing x[i] without first checking ((i in x) !is 
null) will come out correctly.

I note that the above works even if uint[size_t] is changed to real[size_t], 
which is a surprise, because I'd have assumed that everything would come out as 
nan's.


More information about the Digitalmars-d-learn mailing list