Overloading the assignment operator, right now!

Hasan Aljudy hasan.aljudy at gmail.com
Sat Sep 16 13:05:20 PDT 2006



Derek Parnell wrote:
> 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.

Of course it's more ugly, and yes, it's more combuersome to type, even 
though it looks "shorter". Typing := involves pressing "shift" and also 
involves pressing a button that's a bit too far away from one's fingers.

> 
> And we would implemented as ????

I think that's not possible in D right now, but maybe if there was 
something like:

class Foo
{
     namespace dup  //or scope dup{ .... }
     {
         Foo deep() { ... }
         Foo shallow() { ... }
     }
}

> 
>   class Foo
>   {
>      Foo dup()
>      {
>          Foo deep()
>          { ... }
> 
>          Foo shallow()
>          { ... }
>       }
>   }
> 
> Amazingly simple, no?

No, it's not.

> 
> On second thoughts, no thank you.
> 

You're welcome.



More information about the Digitalmars-d mailing list