May 28, 2004
In article <c96240$2cc5$1@digitaldaemon.com>, Derek Parnell says...
>
>Maybe the compiler can check if the class has a 'dup' method and use that if it does exist?

Actually, I've never completely understood the difference between a "dup" method and a copy constructor. Ok, so A.dup(parameters) calls one function and new A(parameters) calls a different function, and they /might/ be defined to do different things (just as opAdd and opAddAssign /might/ be defined to do different things, but that would be counter to common sense). So shouldn't a copy constructor and dup both DO the same thing? Why wouldn't they?

I mean, if I were implementing dup, I'd implement it as:

>       class A
>       {
>           this(A a)
>           {
>               // initialize this by taking details from a
>           }
>
>           A dup()
>           {
>               return new A(this);
>           }
>       }

What other meaning is there for a copy constructor other than to duplicate something? And doesn't that make dup redundant?

(Of course, a.dup is less typing than new A(dup), for those who care about such
things).

Arcane Jill


May 28, 2004
On Fri, 28 May 2004 07:10:48 +0000 (UTC), Arcane Jill wrote:

> In article <c96240$2cc5$1@digitaldaemon.com>, Derek Parnell says...
>>
>>Maybe the compiler can check if the class has a 'dup' method and use that if it does exist?
> 
> Actually, I've never completely understood the difference between a "dup" method and a copy constructor. Ok, so A.dup(parameters) calls one function and new A(parameters) calls a different function, and they /might/ be defined to do different things (just as opAdd and opAddAssign /might/ be defined to do different things, but that would be counter to common sense). So shouldn't a copy constructor and dup both DO the same thing? Why wouldn't they?
> 
> I mean, if I were implementing dup, I'd implement it as:
> 
>>       class A
>>       {
>>           this(A a)
>>           {
>>               // initialize this by taking details from a
>>           }
>>
>>           A dup()
>>           {
>>               return new A(this);
>>           }
>>       }
> 
> What other meaning is there for a copy constructor other than to duplicate something? And doesn't that make dup redundant?
> 
> (Of course, a.dup is less typing than new A(dup), for those who care about such
> things).
> 

Sorry for the confusion. It could be argued that 'dup' is not the same as a copy constructor because it doesn't actually *construct* anything. It assumes that the object already exists and just copies the bits across to the existing target location.

Anyhow, 'dup' is a D term and 'copy constructor' is a C++/Java term.

I don't really care either way, just so long as the bigger picture is still
in focus (namely ':=').


-- 
Derek
28/May/04 5:50:56 PM
May 28, 2004
Woops! My bad.

>> >> >       a.length = b.length;
>> >> >       a[] = b[];

That's a bug. Surprised no-one spotted it. (Actually, no, I'm I not - /I/ didn't spot it until just now).

Of course, the only correct way to copy an array by value is this:

>       a = null;              // Very important statement
>       a.length = b.length;
>       a[] = b[];

Without that all important first line, you could easily be corrupting something else, if a happened to be a shared reference. (I hope the compiler doesn't optimize it away!)

The fact that it takes three separate statements to safely copy an array is certainly an argument in favor of a simpler syntax for copy-by-value, methinks.

Arcane Jill


May 28, 2004
Arcane Jill wrote:

>In article <c96240$2cc5$1@digitaldaemon.com>, Derek Parnell says...
>  
>
>>Maybe the compiler can check if the class has a 'dup' method and use that
>>if it does exist? 
>>    
>>
>
>Actually, I've never completely understood the difference between a "dup" method
>and a copy constructor. 
>
Walters reasoning for a dup instead of a copy constructor was that you never know what a copy constructor is going to be used for (is it a deep copy, shallow copy ect...).  At least with methods like dup you can be more sure its going to make a duplicate and you can always define other copy methods for other types of copies if nessary.

-- 
-Anderson: http://badmama.com.au/~anderson/
May 28, 2004
ok syntactic sugar is often very nice, but it almost always causes more complications and hassle for the compiler writer than it deserves. As well as making the language more complicated therefore more prone to bugs.

In article <c945ff$1q4a$1@digitaldaemon.com>, Arcane Jill says...
>
>One final(ish) push for :=
>
>I have suggested that "a := b;" be rewritten by the compiler as "a = new A(b)". I'm told that this would be merely syntactic sugar.
>
>Well yeah, it is. But the phrase "syntactic sugar" is a loaded phrase, implying "bad thing". The fact is that we can write "a = b + c" instead of "a = b.opAdd(c)", and we LIKE it. Syntactic sugar is sometimes nice.
>
>It's a very simple idea:
>
>>       a = b;      // copy by reference
>>       a := b;     // copy by value
>
>This syntactic sugar would ALSO allow you to things like:
>
>>       Int n := 5;
>
>(Int is my big integer class) instead of:
>
>>       Int n = new Int(5);
>
>That sugary code is much more /smooth/. It /flows/.
>
>If no-one likes this idea, I'll drop it, but realise that this would also work for arrays. That is, if a and b are arrays, then
>
>>       a := b;
>
>would be equivalent to:
>
>>       a.length = b.length;
>>       a[] = b[];
>
>And as for structs, there would be no harm in allowing structs to have constructors (but not destructors), so it could work for them too.
>
>Arcane Jill (throwing caution to the wind).
>
>


May 28, 2004
Arcane Jill wrote:

> ... the only correct way to copy an array by value is this:
> 
> 
>>      a = null;              // Very important statement
>>      a.length = b.length;
>>      a[] = b[];
> 
> 
> Without that all important first line, you could easily be corrupting something
> else, if a happened to be a shared reference. (I hope the compiler doesn't
> optimize it away!)
> 
> The fact that it takes three separate statements to safely copy an array is
> certainly an argument in favor of a simpler syntax for copy-by-value, methinks.

What about a=b.dup;?

 -- andy
May 29, 2004
In article <c97tu8$2385$2@digitaldaemon.com>, Andy Friesen says...
>
>What about a=b.dup;?
>
>  -- andy

You're right. I missed that one.

Jill


May 30, 2004
"J Anderson" <REMOVEanderson@badmama.com.au> escribió en el mensaje
news:c97426$t2o$1@digitaldaemon.com
| Walters reasoning for a dup instead of a copy constructor was that you
| never know what a copy constructor is going to be used for (is it a deep
| copy, shallow copy ect...).  At least with methods like dup you can be
| more sure its going to make a duplicate and you can always define other
| copy methods for other types of copies if nessary.
|
| --
| -Anderson: http://badmama.com.au/~anderson/

Sorry for asking, but what's the difference between a deep copy and a shallow copy? Then only case I can think of something like this:

class A { A dup () { return new A; } }
class B
{
    A a;
    B shallow_dup ()
    {
        B b = new B;
        b.a = a;
        return b;
    }
    B deep_dup ()
    {
        B b = new B;
        b.a = a.dup;
        return b;
    }
}

Have I guessed right?

-----------------------
Carlos Santander Bernal


May 30, 2004
Carlos Santander B. wrote:

>"J Anderson" <REMOVEanderson@badmama.com.au> escribió en el mensaje
>news:c97426$t2o$1@digitaldaemon.com
>| Walters reasoning for a dup instead of a copy constructor was that you
>| never know what a copy constructor is going to be used for (is it a deep
>| copy, shallow copy ect...).  At least with methods like dup you can be
>| more sure its going to make a duplicate and you can always define other
>| copy methods for other types of copies if nessary.
>|
>| --
>| -Anderson: http://badmama.com.au/~anderson/
>
>Sorry for asking, but what's the difference between a deep copy and a
>shallow copy? Then only case I can think of something like this:
>
>class A { A dup () { return new A; } }
>class B
>{
>    A a;
>    B shallow_dup ()
>    {
>        B b = new B;
>        b.a = a;
>        return b;
>    }
>    B deep_dup ()
>    {
>        B b = new B;
>        b.a = a.dup;
>        return b;
>    }
>}
>
>Have I guessed right?
>
>-----------------------
>Carlos Santander Bernal
>  
>

Right, but a shallow copy can be as low as a pointer copy which happens now.  How shallow is shallow and how deep is deep.

-- 
-Anderson: http://badmama.com.au/~anderson/
1 2
Next ›   Last »