D - more or less power than C++?

Derek Parnell derek at psych.ward
Sat Mar 4 05:28:34 PST 2006


On Sun, 05 Mar 2006 00:25:16 +1100, Ben Phillips <Ben_member at pathlink.com>  
wrote:

> In article <dubfs7$95p$1 at digitaldaemon.com>, Andrew Fedoniouk says...
>>
>> I am not exactly sure what this means: assign-at-compile-time,  
>> assign-once.
>> Could you provide some examples?
>>
>> I mean something like
>>
>> struct cow_string {
>>   ref_counted_buffer data;
>>   void opAssign(cow_string s)
>>   {
>>       release(data);
>>       data = s.data;
>>       addref(data);
>>   }
>> }
>>
>
> Since D uses references for all classes, providing opAssign is just way  
> to
> dangerous to be worth the effort. Lets say you have a class like follows:
>
> class Array
> {
> private int[] array;
> public this()
> {
> array = new int[10];
> }
> void opAssign(Array a)
> {
> for (int i = 0; i < a.length; i++)
> array[i] = a[i];
> }
> }
>
> and lets say I write something like this:
> Array a = new Array();
> Array b = new Array();
> a = b;
>
> I expect a to refer to the same object as b, but no. The author of the  
> Array
> class copies the data in the overloaded operator, so my code doesn't  
> work for
> seemingly unknown reasons. This is a problem which would require more  
> than just
> a "don't do it" warning.
>
> Imho, we just need a standardized "clone" method (like .dup) that can be  
> used
> for assigning.

YES! And a syntax to support opDup().  The "=" means assign, so why not  
have another operator to mean copy-the-data-not-just-the-reference? ":="  
has been suggested but there could be other great ideas.


-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d mailing list