opIndexAddAssign? ...

Sean Kelly sean at f4.ca
Fri Feb 9 08:27:56 PST 2007


Lionello Lunesu wrote:
> Manfred Nowak wrote:
>> On writing the implementation of a map, where the Valuetype is a set, 
>> I had to stop on recognizing, that only opIndexAssign is permitted.
>>
>> I.e.:
>>   map[ key]+= element;
>> has no possible overload.
>>
>> OpIndex delivers an rvalue and the only possibility to get an lvalue 
>> is in conjunction with a plain assignment.
>>
>> Why arbitrary restrictions when the goal is to have great expressiveness?
>> -manfred
> 
> opIndexAssign was needed because D has no "inout" return type. It's a 
> hack, if you ask me.

It is a hack, but a useful one IMO.  For example, say I want to insert 
an element in an AA only if the AA itself is used as an lvalue. ie.

auto x = myAA[key]; // return default value if not present
myAA[key] = x;      // insert and assign

I'll admit that it would be much nicer if this worked for all applicable 
operator overloads without requiring separate signatures for each, but 
I'll take what I can get :-)


Sean



More information about the Digitalmars-d mailing list