www.digitalmars.com         C & C++   DMDScript  

D - Asssociative array Exception?

I had an associative array with class objects as the values, and I'd use the
"in" operator in several places to see if there were particular keys in the
array, then access aarr[foo] to access the class object and pass it to a
function or something. In a couple places, I forgot to check with "in", if
it existed, and it ended up adding a null ref to the array, and so it would
crash when accessing members. It took me a little while to find out how null
references got into the array, but I was thinking that it could throw an
exception if the key wasn't in the array. It would force you to test if it
was in there using "in" if you weren't sure, and explicitly add a value if
you wanted one in there.

Object[int] aarr;
aarr[3] = new Object; /* as usual */
if(4 in aarr) something(aarr[4]);  /*  safe  */
something(aarr[5]);  /* oops */

Sure it was a pretty stupid mistake of mine, but this could be considered a
type of "bounds checking" ;)
Sep 13 2003