Why do associative arrays return from opIndexAssign by value?
deadalnix
deadalnix at gmail.com
Tue Jun 18 02:07:30 PDT 2013
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.
AA are screwed in much deeper way that that simple use case, and
I do not think adding the feature right now is a wise move.
More information about the Digitalmars-d
mailing list