digitalmars.D - Copy by value semantics
- Arcane Jill (16/23) May 27 2004 One final(ish) push for :=
- DemmeGod (4/42) May 27 2004 mmmmm.... sugar... so sweet....
- Matthew (8/31) May 27 2004 Sorry. I must strongly disagree with you. For a variety of reasons, D ha...
- DemmeGod (2/55) May 27 2004
- Matthew (17/72) May 27 2004 Alas I cannot.
- Arcane Jill (10/15) May 28 2004 That's a bug. Surprised no-one spotted it. (Actually, no, I'm I not - /I...
- Andy Friesen (3/17) May 28 2004 What about a=b.dup;?
- Arcane Jill (3/5) May 29 2004 You're right. I missed that one.
- Derek Parnell (19/26) May 27 2004 Que? Strongly disagree with what? Its only a syntax change not a semanti...
- Arcane Jill (13/26) May 28 2004 Actually, I've never completely understood the difference between a "dup...
- Derek Parnell (11/44) May 28 2004 Sorry for the confusion. It could be argued that 'dup' is not the same a...
- J Anderson (8/16) May 28 2004 Walters reasoning for a dup instead of a copy constructor was that you
-
Carlos Santander B.
(32/32)
May 29 2004
"J Anderson"
escribió en el mensaje - J Anderson (5/38) May 29 2004 Right, but a shallow copy can be as low as a pointer copy which happens
- Juan C (1/4) May 27 2004 Yech. I still want := to be the assignment operator, and a bare = be inv...
- Ben Hinkle (13/16) May 27 2004 A(b)".
- Derek Parnell (24/56) May 27 2004 I'm with you on this one too. What you are suggesting is a SHORTHAND met...
- James McComb (5/9) May 27 2004 I like the way the word "new" alerts me to fact that I'm creating a new
- imr1984 (4/27) May 28 2004 ok syntactic sugar is often very nice, but it almost always causes more
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 valueThis 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, thena := 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 27 2004
mmmmm.... sugar... so sweet.... I like the idea. Why anyone would object to it? (I'm sure I'll find out soon enough) On Thu, 27 May 2004 07:30:55 +0000, Arcane Jill wrote: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 valueThis 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, thena := 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 27 2004
Sorry. I must strongly disagree with you. For a variety of reasons, D has opted to represent objects as references to instances. Given that I think it in appropriate to support any kind of implicit value semantics. It just leads to too many subtle contradictions. I recognise and agree that it can be inconvenient, but it's just impossible to have a perfect language, and the cost benefit for this feature doesn't warrant it. "Arcane Jill" <Arcane_member pathlink.com> wrote in message news:c945ff$1q4a$1 digitaldaemon.com...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 valueThis 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, thena := 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 27 2004
I don't see any of those subtle contradictions... can you give an example? On Thu, 27 May 2004 18:00:35 +1000, Matthew wrote:Sorry. I must strongly disagree with you. For a variety of reasons, D has opted to represent objects as references to instances. Given that I think it in appropriate to support any kind of implicit value semantics. It just leads to too many subtle contradictions. I recognise and agree that it can be inconvenient, but it's just impossible to have a perfect language, and the cost benefit for this feature doesn't warrant it. "Arcane Jill" <Arcane_member pathlink.com> wrote in message news:c945ff$1q4a$1 digitaldaemon.com...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 valueThis 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, thena := 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 27 2004
Alas I cannot. I'm one of those vexing people who do a lot of things subconsciously, and often have just "feelings" about things for ages. I've had qualms about certain things that have literally taken months to bubble up through my tangled grey matter. The way it works for me is that I'll remember that there are misgivings about this issue, and when I come across the related issue(s) on which those misgivings are (unconsciously) founded, the bells will ring, and I'll rush back and write it down, or post an NG message. This works well for writing books/articles, and for developing software, but it can be quite a problem when having debates in person, or on newsgroups. Given that, please feel free to ignore me as an irritating prick! :-) (I'm going a bike ride in a minute, and that often helps things bubble up ...) "DemmeGod" <me demmegod.com> wrote in message news:pan.2004.05.27.21.56.47.935562 demmegod.com...I don't see any of those subtle contradictions... can you give an example? On Thu, 27 May 2004 18:00:35 +1000, Matthew wrote:Sorry. I must strongly disagree with you. For a variety of reasons, D has opted to represent objects as references to instances. Given that I think it in appropriate to support any kind of implicit value semantics. It just leads to too many subtle contradictions. I recognise and agree that it can be inconvenient, but it's just impossible to have a perfect language, and the cost benefit for this feature doesn't warrant it. "Arcane Jill" <Arcane_member pathlink.com> wrote in message news:c945ff$1q4a$1 digitaldaemon.com...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 valueThis 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, thena := 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 27 2004
Woops! My bad.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.length = b.length; a[] = b[];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:... the only correct way to copy an array by value is this:What about a=b.dup;? -- andya = 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.
May 28 2004
In article <c97tu8$2385$2 digitaldaemon.com>, Andy Friesen says...What about a=b.dup;? -- andyYou're right. I missed that one. Jill
May 29 2004
On Thu, 27 May 2004 18:00:35 +1000, Matthew wrote:Sorry. I must strongly disagree with you. For a variety of reasons, D has opted to represent objects as references to instances. Given that I think it in appropriate to support any kind of implicit value semantics. It just leads to too many subtle contradictions. I recognise and agree that it can be inconvenient, but it's just impossible to have a perfect language, and the cost benefit for this feature doesn't warrant it.Que? Strongly disagree with what? Its only a syntax change not a semantic one. It doesn't break any exiting code. It doesn't make life harder. I'm not sure what you mean by "conrtadiction". Are you saying that, in some situations, you can envisage that the phrase " a := b " will not mean what we expect it to mean? It will always mean " Set the value of whatever 'a' refers to, to be identical to the value of whatever 'b' refers to". In other words, make a duplicate of b's object. If 'b' is an integer then take a copy of the integer value. If 'b' is a class object then create a new object of the same class, copy the RAM values to the new object and then assign 'a' to refer to the new object. Just like you might do in longhand. Maybe the compiler can check if the class has a 'dup' method and use that if it does exist? We aren't expecting the "perfect language", just a better one. And which cost-benefit study are you referring to? What are the costs? What are the benefits? And how do you measure them against each other? -- Derek 28/May/04 10:32:46 AM
May 27 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...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 PMMaybe 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).
May 28 2004
Arcane Jill wrote:In article <c96240$2cc5$1 digitaldaemon.com>, Derek Parnell says...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/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.
May 28 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 29 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 BernalRight, 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/
May 29 2004
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.Yech. I still want := to be the assignment operator, and a bare = be invalid.
May 27 2004
"Arcane Jill" <Arcane_member pathlink.com> wrote in message news:c945ff$1q4a$1 digitaldaemon.com...One final(ish) push for := I have suggested that "a := b;" be rewritten by the compiler as "a = newA(b)".I'm told that this would be merely syntactic sugar.Is A the dynamic type or the static type? In other words if "b" is an instance of a subclass of A would "a" be an instance of the same subclass? If the compiler hard-codes in "new A(b)" then it looks like it wouldn't be. One way to get "value semantics" that gives the subclass complete control is to use a ".dup" property a = b.dup; The biggest difference from your proposal is that the class writer would have to define the .dup property instead of having the compiler generate one. I don't think that is so bad since dup'ing is fairly rare. -Ben
May 27 2004
On Thu, 27 May 2004 07:30:55 +0000 (UTC), Arcane Jill wrote: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.I'm with you on this one too. What you are suggesting is a SHORTHAND method of doing something which is commonly written out in long hand. You are not saying that the current way should be abandonded, just supplemented with an alternative to make life easier for coders, and readers of code alike.It's a very simple idea:Yes. Nothing to taxing for my brain here. Sometimes we need to copy the reference to 'X' and sometimes we need to copy the value contained in 'X'. Or in other words, using the examples above, in the first one we want to copy what 'b' is, and in the second we want to copy what 'b' refers to. For simple datatypes (int, float, char, etc...) the two are identical in meaning. The difference appears when we are talking about classes, arrays, and structs.a = b; // copy by reference a := b; // copy by valueThis syntactic sugar would ALSO allow you to things like:Yep, sure does. And it doesn't stop somebody from coding the long way or from coding ... Int n = new BaseInt(5); when Int and BaseInt are related.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, thenMuch shorter and to the point. You are simply saying that you wish to make a duplicate of the array 'b'.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.Still makes sense to me. I'm all in favour in making writing code and reading code easier. -- Derek 28/May/04 10:19:50 AM
May 27 2004
Arcane Jill wrote: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.I like the way the word "new" alerts me to fact that I'm creating a new object. Also, if I'm wondering where I create new objects, I can just search for "new". James McComb
May 27 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 valueThis 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, thena := 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