associative arrays

Jonathan M Davis jmdavisProg at
Sat Jan 7 15:59:05 PST 2012

On Saturday, January 07, 2012 23:34:05 RenatoL wrote:
> Yes, i agree this may acceptable. On the other hand if i really
> want/have to remove an item i have to be very careful cause a
> trivial typo could cause a disaster....

In general, if an element isn't in a container, and you expect it to be there, 
it's bug in your code and _should_ cause disaster. That is, if your code is 
assuming that the element must be in the container before you remove it, you 
should be asserting for that.

assert(key in aa);

This will result in an AssertError if key is not in aa, and upon failure will 
indicate that you have a bug in your code that you need to fix.

If, on the other hand, it's not a bug if that element is not in the container, 
then why would your code be assuming that it is? In that case, I don't see why 
you'd care whether it was actually in the container or not, and having remove 
not do anything if it isn't there is the perfect behavior in that case.

So, I'd argue that if your code is assuming that the element is in the 
container, an assertion should be used, and the fact that remove doesn't throw 
an exception is irrelevant, since it would be the wrong thing to do anyway. 
And if your code can't assume that the element is in the container already, 
then just let remove remove it if it's there and do nothing if it's not.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list