Overloading the assignment operator, right now!

Derek Parnell derek at psyc.ward
Sat Sep 16 04:18:47 PDT 2006


On Fri, 15 Sep 2006 20:19:48 -0600, Hasan Aljudy wrote:

> Derek Parnell wrote:
>> 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?
>> 
> 
> I vote:
> No for copy operators.
> Specially no for the ugly and cumbersome-to-type := operator.
> 
> Yes for auto-generated copy methods/properties. (both deep and shallow)
> 
> T x = b.dup.deep;
> T y = b.dup.shallow;

Oh I see ...  ":=" is obviously more ugly and more cumbersome than
".dup.deep". Don't know how I didn't see that.

And we would implemented as ????

  class Foo
  {
     Foo dup()
     {
         Foo deep()
         { ... }

         Foo shallow()
         { ... }
      }
  }

Amazingly simple, no?

On second thoughts, no thank you.

-- 
Derek Parnell
Melbourne, Australia
"Down with mediocrity!"



More information about the Digitalmars-d mailing list