Overloading the assignment operator, right now!

Derek Parnell derek at nomail.afraid.org
Fri Sep 15 01:18:01 PDT 2006


On Fri, 15 Sep 2006 09:57:53 +0200, Oskar Linde wrote:

> Georg Wrede wrote:
>> Hasan Aljudy wrote:
>>> 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.
>>>
>>> <snip>
>>>
>>> Why do you have to think in C++ when coding in D?
>> 
>> I don't. Do you?
>> 
>> Seriously, if you look back in the archives, I've been pretty voluminous 
>> in my support of several ideas and opinions. Getting assignment operator 
>> overload hasn't been one of them.
>> 
>> Then again, some other people have really wanted it, and I stumbled on a 
>> method to try it out with, which I wrote here. Now they can try it out, 
>> without Walter having to make changes to the language.
> 
> There is more to it than having an operator to overload. You want to be 
> able to overload both assignment and copying.
> 
> Given
> 
> struct T { ... opCatAssign(T t) {...} }
> void func(T t) {...}
> T a,b;
> 
> You take care of
> 
> 	a ~= b;

  Yes, with  'a.opCatAssign(b);'
 
> but not
> 
> 	func(b);
> 
> where a copy is created too.

Are you saying that a simple struct reference in a function call just does
a shallow copy of the struct, but that sometimes you might need to do a
deep copy instead?

What about ...

  func( T x := b);

assuming for the sake of this example that ':=' is the (deep) copy
operator?

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
15/09/2006 6:06:49 PM



More information about the Digitalmars-d mailing list