Why do associative arrays return from opIndexAssign by value?

TommiT tommitissari at hotmail.com
Tue Jun 18 02:03:26 PDT 2013


Associative arrays return from opIndexAssign the inserted or 
existing element by value. Why don't they return it by reference, 
which would be much more useful? Maybe not very common, but 
here's an example of the kind of situation where I actually would 
have needed it:

int[string] values;

foreach (string key; keys)
{
     int* ptr = key in values;

     if (ptr == null)
     {
         // Currently cannot write this:
         // ptr = &(values[key] = initValueFor(key));

         // The workaround is inefficient:
         values[key] = initValueFor(key);
         ptr = key in values;
     }
     edit(*ptr);
}


More information about the Digitalmars-d mailing list