Overloading the assignment operator, right now!

Georg Wrede georg.wrede at nospam.org
Thu Sep 14 07:30:29 PDT 2006


Overloading of the assignment operator is not allowed in D.

But no problem, we can do it right now, in spite of it.

Just overload the opCatAssign operator instead, and use ~= where = would 
be used.

This way we might gather enough use cases to really know whether it is 
good or bad in practice.

The ~= looks peculiar enough to stick in the eye, and therefore the 
chances of mixups and misunderstandings are small. Since both writing 
the overloads and using the operator are exactly the same as with the 
"real" assignment operator overload, the lessons learned are immediately 
applicable if we later want to reconsider the omission of it.

Of course, I'm not suggesting using this in published code, or example 
code! But for "private" code, this may be an interesting addition to the 
mental toolbox.

I think it is quite unusual to concatenate two "free standing" objects. 
Concatenation is only used with strings, or to join objects to arrays, 
or two arrays to each other. Therefore using this operator (as a 
substitute for the regular assignment operator) in a non-array context 
should not bring too much of a mental strain for the reader.

---

One could use any binary operator here, but it is much simpler if the 
same one is used by all programmers.



More information about the Digitalmars-d mailing list