associative arrays

Stephen Bennett d at
Sun Jan 8 00:02:21 PST 2012

On 1/7/2012 8:54 PM, bearophile wrote:
>> Yes, Jonathan, you're right.
>> the question arose precisely from a typo... i had to remove an
>> item with key "length"... i wrote "lengt" and the item never went
>> away... i knew that "lengt" was not in my key list... This kind of
>> mistake is quite tricky, may be using and IDE could help.
> This an example that shows that silent failures are sources of bugs...
> So this time I don't agree with Jonathan Davis.

a.remove(x) doesn't fail though. It successfully satisfies its own
postconditions by placing a in a state whereby it no longer contains x.
The real source of bugs in this example is using hardcoded strings
instead of named constants. :)

Besides, one frequently doesn't know if (x in a) or not ahead of time.
Think of evicting some computed intermediate results from a cache when
you detect that the underlying resource has changed, for instance. In
fact, it's really rare for an item to be removed from a long-lived
collection anywhere near the point that it was added. Insisting on
existence before removal clutters up the code for an extremely common
use case.

FWIW, Java's Map and .NET's IDictionary also handle this situation silently.


More information about the Digitalmars-d-learn mailing list