opIndexAddAssign? ...

BCS BCS at pathlink.com
Fri Feb 9 11:40:42 PST 2007


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

It could have some nasty side effect but, have opIndex return a 
struct/object that shadows the original and overloads opAddAssign.

class F
{
	T[] i
	class N
	{
		this(inout T l){j=&l;}
		T* j;
		T opAddAssign(T k)
		{
			j+=k;
			return this;
		}
	}

	N opIndex(int ind)
	{
		return new N(i[ind]);
	}
}

On second thought, don't



More information about the Digitalmars-d mailing list