Why do associative arrays return from opIndexAssign by value?
TommiT
tommitissari at hotmail.com
Tue Jun 18 03:35:06 PDT 2013
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);
}
More information about the Digitalmars-d
mailing list