D - more or less power than C++?

Ben Phillips Ben_member at pathlink.com
Sat Mar 4 05:25:16 PST 2006


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.





More information about the Digitalmars-d mailing list