On Tuesday, 18 June 2013 at 09:07:31 UTC, deadalnix wrote:
On Tuesday, 18 June 2013 at 09:03:27 UTC, TommiT wrote:
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);
}

Assignation aren't lvalues in general, so that is consistent.

I didn't see that coming. In C++ assignments are pretty consistently mutable lvalues. Do you know why this is so in D?

Reply via email to