How to modify an element in a range/collection using its member function?

Peter Alexander peter.alexander.au at gmail.com
Thu May 3 04:24:34 PDT 2012


I don't think that opIndexAssign/opIndexOpAssign were ever a good 
idea. Yeah, it works fine when all you are doing is x[i] = y, but 
that doesn't scale, as you have found out. It's short-sighted.

x[i] is an L-value for arrays, so it should be for everything 
else where it makes sense. Consistency is paramount.

I know C++ had the problem with std::map where x[i] would 
construct an entry when x[i] didn't exist, but that was due to a 
bad design decision. Indexing should never perform an insertion. 
Force users to use an 'insert' function to add elements and 
indexing should only work when the key is found (throw an 
exception otherwise). Again, it's a matter of consistency. 
Accessing x[10] of a 5-element array doesn't expand the array, so 
accessing x["foo"] of an empty map shouldn't expand the map.

Am I missing something here?


More information about the Digitalmars-d mailing list