Overloading the assignment operator, right now!

J Duncan jtd514 at nospam.ameritech.net
Thu Sep 14 09:48:40 PDT 2006


Georg Wrede wrote:
> 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.

I agree, I have been using =~ in some cases for assignment for close to 
a year now. Its decent, and it works. And as we were discussing on #D 
one day, its ironic that a decision made by walter designed to 
un-obfuscate code might end up causing developers to implement 
work-arounds like this, resulting in MORE obfuscated code......

go figure... :)



More information about the Digitalmars-d mailing list