Principled method of lookup-or-insert in associative arrays?

Fawzi Mohamed fawzi at gmx.ch
Sun Nov 21 10:41:35 PST 2010


I was thinking about this, and revised my idea.
While I think that in general hiding idup is a bad practice, I think  
that for AA one could make the case that it might be acceptable.
idup is needed only when assigning a value using a key that isn't  
immutable.
Now I think that this use in AA is relatively seldom, one normally  
wants to read more than write, so some magic might be warranted.
If one has a use case where the the idup really becomes an issue he  
should expend the extra thinking to avoid it.
The advantage of this is that idup is not needed for basic operations,  
which I think is good, as one doesn't have to confront himself with  
the const complexity so early when using the language. const should be  
kid of avoidable in several tasks if one doesn't care about it.
What I am still uneasy about the original example is its reliance on  
the automatic initialization to 0 for undefined keys, an AA normally  
throws when the key is not present, it doesn't return the default  
value, why should updating be different?.

Fawzi



More information about the Digitalmars-d mailing list