digitalmars.D.learn - Copying structs via function (not bitwise)
- Matthias Walter (6/6) Feb 14 2008 Is there any other way of copying structs with '=' operator, other than ...
- Bill Baxter (13/23) Feb 14 2008 opAssign can be used to override the meaning of A=B where A and B are
- Matthias Walter (12/40) Feb 14 2008 Problem is that GMP-structs should behave like normal numbers and one al...
- Bill Baxter (8/53) Feb 14 2008 Yep. That's what it means. I don't know what's in gmp_struct but if it...
- Matthias Walter (4/60) Feb 14 2008 Okay, then I know on it for now. The problem is indeed, that the C-libra...
- Bill Baxter (11/72) Feb 14 2008 You can't make natural syntax work for everything but you _can_ make
- Jarrett Billingsley (3/5) Feb 14 2008 You say that like putting spoons in your eyes is a bad thing!
- Bill Baxter (4/11) Feb 14 2008 I'm quite fond of a spoon in the ear at least.
- downs (3/17) Feb 15 2008 How about keeping to D conventions, and using j = i.dup?
- Matthias Walter (12/31) Feb 16 2008 Do you code all your stuff with D conventions - _also_ for numbers?
Is there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias Walter
Feb 14 2008
Matthias Walter wrote:Is there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias WalteropAssign can be used to override the meaning of A=B where A and B are different types. But by design you can't change the meaning A=B where A and B are the same type. But you could make it work using any of the following: A ~= B; A.foo = B; A = B.foo; assign(A,B); A.assign(B); etc.. And I'm sure downs could suggest a few more workable syntaxes. :-) --bb
Feb 14 2008
Bill Baxter Wrote:Matthias Walter wrote:Problem is that GMP-structs should behave like normal numbers and one also does not write: int i; i.value = 10; Does this mean, that in the current situation, it is impossible to port the GMP library with structs so that T i,j = 0; i = j; j++; assert (i == 0); works for T = int, T = long, T = float, T = gmp_struct ? best regards Matthias WalterIs there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias WalteropAssign can be used to override the meaning of A=B where A and B are different types. But by design you can't change the meaning A=B where A and B are the same type. But you could make it work using any of the following: A ~= B; A.foo = B; A = B.foo; assign(A,B); A.assign(B); etc.. And I'm sure downs could suggest a few more workable syntaxes. :-) --bb
Feb 14 2008
Matthias Walter wrote:Bill Baxter Wrote:Yep. That's what it means. I don't know what's in gmp_struct but if it includes a pointer to something that must be allocated then D ain't gonna be able to do it. But D2 is supposedly going to be getting features to make ref-counting smart pointer structs possible, and I'm pretty sure that means there has to be some way to trigger side effects upon assignments. --bbMatthias Walter wrote:Problem is that GMP-structs should behave like normal numbers and one also does not write: int i; i.value = 10; Does this mean, that in the current situation, it is impossible to port the GMP library with structs so that T i,j = 0; i = j; j++; assert (i == 0); works for T = int, T = long, T = float, T = gmp_struct ? best regards Matthias WalterIs there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias WalteropAssign can be used to override the meaning of A=B where A and B are different types. But by design you can't change the meaning A=B where A and B are the same type. But you could make it work using any of the following: A ~= B; A.foo = B; A = B.foo; assign(A,B); A.assign(B); etc.. And I'm sure downs could suggest a few more workable syntaxes. :-) --bb
Feb 14 2008
Bill Baxter Wrote:Matthias Walter wrote:Okay, then I know on it for now. The problem is indeed, that the C-library for GMP must allocate some memory for the number, meaning you always only maintain some pointers to that memory. bitwise-copy would then lead to a double reference to the same number... best regards Matthias WalterBill Baxter Wrote:Yep. That's what it means. I don't know what's in gmp_struct but if it includes a pointer to something that must be allocated then D ain't gonna be able to do it. But D2 is supposedly going to be getting features to make ref-counting smart pointer structs possible, and I'm pretty sure that means there has to be some way to trigger side effects upon assignments. --bbMatthias Walter wrote:Problem is that GMP-structs should behave like normal numbers and one also does not write: int i; i.value = 10; Does this mean, that in the current situation, it is impossible to port the GMP library with structs so that T i,j = 0; i = j; j++; assert (i == 0); works for T = int, T = long, T = float, T = gmp_struct ? best regards Matthias WalterIs there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias WalteropAssign can be used to override the meaning of A=B where A and B are different types. But by design you can't change the meaning A=B where A and B are the same type. But you could make it work using any of the following: A ~= B; A.foo = B; A = B.foo; assign(A,B); A.assign(B); etc.. And I'm sure downs could suggest a few more workable syntaxes. :-) --bb
Feb 14 2008
Matthias Walter wrote:Bill Baxter Wrote:You can't make natural syntax work for everything but you _can_ make unnatural syntax work for everything. I.e. you could make it so that: T i,j = 0; assign(i,j); j++; assert(i==0); works for all the T's you want it to. Not quite as sweet as having it all appear to be built-in but better than a spoon in the eye. --bbMatthias Walter wrote:Okay, then I know on it for now. The problem is indeed, that the C-library for GMP must allocate some memory for the number, meaning you always only maintain some pointers to that memory. bitwise-copy would then lead to a double reference to the same number... best regards Matthias WalterBill Baxter Wrote:Yep. That's what it means. I don't know what's in gmp_struct but if it includes a pointer to something that must be allocated then D ain't gonna be able to do it. But D2 is supposedly going to be getting features to make ref-counting smart pointer structs possible, and I'm pretty sure that means there has to be some way to trigger side effects upon assignments. --bbMatthias Walter wrote:Problem is that GMP-structs should behave like normal numbers and one also does not write: int i; i.value = 10; Does this mean, that in the current situation, it is impossible to port the GMP library with structs so that T i,j = 0; i = j; j++; assert (i == 0); works for T = int, T = long, T = float, T = gmp_struct ? best regards Matthias WalterIs there any other way of copying structs with '=' operator, other than the default way of a bitwise copy? I quote the FAQ: "Structs, being value objects, do get copied about. A copy is defined in D to be a bit copy. I've never been comfortable with any object in C++ that does something other than a bit copy when copied. Most of this other behavior stems from that old problem of trying to manage memory. Absent that, there doesn't seem to be a compelling rationale for having anything other than a bit copy." The reason, I'm asking for this, is that I'm working on a GMP port. Because numbers _must_ be value types, I must use structs. But as different instances of the same number must reference another memory region of the GMP internals, I must call special copy functions, if I do "a = b;" on two gmp-structs, a refers to the same gmp-internal memory as b. Is there any way to do this? best regards Matthias WalteropAssign can be used to override the meaning of A=B where A and B are different types. But by design you can't change the meaning A=B where A and B are the same type. But you could make it work using any of the following: A ~= B; A.foo = B; A = B.foo; assign(A,B); A.assign(B); etc.. And I'm sure downs could suggest a few more workable syntaxes. :-) --bb
Feb 14 2008
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fp1hvu$1hom$1 digitalmars.com...Not quite as sweet as having it all appear to be built-in but better than a spoon in the eye.You say that like putting spoons in your eyes is a bad thing!
Feb 14 2008
Jarrett Billingsley wrote:"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fp1hvu$1hom$1 digitalmars.com...I'm quite fond of a spoon in the ear at least. (As long as it's this one: http://www.spoontheband.com/) --bbNot quite as sweet as having it all appear to be built-in but better than a spoon in the eye.You say that like putting spoons in your eyes is a bad thing!
Feb 14 2008
Bill Baxter wrote:You can't make natural syntax work for everything but you _can_ make unnatural syntax work for everything. I.e. you could make it so that: T i,j = 0; assign(i,j); j++; assert(i==0); works for all the T's you want it to. Not quite as sweet as having it all appear to be built-in but better than a spoon in the eye. --bbHow about keeping to D conventions, and using j = i.dup? --downs
Feb 15 2008
downs Wrote:Bill Baxter wrote:Do you code all your stuff with D conventions - _also_ for numbers? | int start = 10; | int end = 20; | | for (i = start.dup; i < end; i++) | { | | } when dealing with integers, you normally don't use .dup, do you? And the sense of GMP is the easy usage of arbitrary precision numbers as they were ints, longs or floats... best regards Matthias WalterYou can't make natural syntax work for everything but you _can_ make unnatural syntax work for everything. I.e. you could make it so that: T i,j = 0; assign(i,j); j++; assert(i==0); works for all the T's you want it to. Not quite as sweet as having it all appear to be built-in but better than a spoon in the eye. --bbHow about keeping to D conventions, and using j = i.dup? --downs
Feb 16 2008