On Tuesday, 18 June 2013 at 10:18:23 UTC, Jonathan M Davis wrote:
On Tuesday, June 18, 2013 11:41:03 TommiT wrote:
At least int assignment isn't lvalue. I find this quite
surprising:

void edit(ref int) { }

void main()
{
     int n;
     //edit(n = 4); // Error
     edit(n += 4);
}

Maybe that's by design, but it's not what I would have expected. I don't know if that's a bug or not. Certainly, it doesn't match how it works in C++, and I
don't know why D would be any different in this regard.

- Jonathan M Davis

This must be by design, because associative arrays return lvalue from += operator as well:

int[string] values;

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

    if (ptr == null)
    {
        // Now it's quite a decent workaround:
        ptr = &(values[key] += 0);
        *ptr = initValueFor(key);
    }
    edit(*ptr);
}

Reply via email to