D - more or less power than C++?

Bruno Medeiros daiphoenixNO at SPAMlycos.com
Sat Mar 4 06:08:42 PST 2006


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

Hum, yes!, ideally we could two pairs of operators, each for 
setting/testing the reference/instance_data

=	Assign the reference to an object (currently: = )
==	Test for identity, aka same reference (currently: is )
:=	Copy the object. (nothing similar)
:==	Test for equality, aka same instance data ( currently: == )

This is just the structural idea, the name/symbols of these operators 
could be something other and better (and I think this choice would be a 
very important aspect of this feature).


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list