www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - .idup flawed?

reply Xinok <xnknet gmail.com> writes:
.idup creates an invariant copy of an array. According to this page:
http://digitalmars.com/d/const.html

"One is that constant data really is constant. It never changes. It's 
different enough that it needs a different name. In D, this kind of 
constant is called an invariant."


So if you have an invariant array, you expect that it's data will never 
change.

This is not always the case if you have an array of classes. Classes are 
pointers, and when you use the .idup property, it only creates invariant 
copies of the pointers, and not the objects themselves. This means that 
the objects are still mutable and the data can change, which is not the 
intended behavior of invariant.
Jun 21 2007
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Xinok wrote:
 .idup creates an invariant copy of an array. According to this page:
 http://digitalmars.com/d/const.html
 
 "One is that constant data really is constant. It never changes. It's 
 different enough that it needs a different name. In D, this kind of 
 constant is called an invariant."
 
 
 So if you have an invariant array, you expect that it's data will never 
 change.
 
 This is not always the case if you have an array of classes. Classes are 
 pointers, and when you use the .idup property, it only creates invariant 
 copies of the pointers, and not the objects themselves. This means that 
 the objects are still mutable and the data can change, which is not the 
 intended behavior of invariant.
I think you might have found a fatal flaw in .idup. Aggh!
Jun 21 2007
parent Jason House <jason.james.house gmail.com> writes:
Walter Bright wrote:
 Xinok wrote:
 .idup creates an invariant copy of an array. According to this page:
 http://digitalmars.com/d/const.html

 "One is that constant data really is constant. It never changes. It's 
 different enough that it needs a different name. In D, this kind of 
 constant is called an invariant."


 So if you have an invariant array, you expect that it's data will 
 never change.

 This is not always the case if you have an array of classes. Classes 
 are pointers, and when you use the .idup property, it only creates 
 invariant copies of the pointers, and not the objects themselves. This 
 means that the objects are still mutable and the data can change, 
 which is not the intended behavior of invariant.
I think you might have found a fatal flaw in .idup. Aggh!
Has shallow and deep copies ever been discussed on this mailing list? One thing that has been bugging me lately is that .dup only does a shallow copy. Making .idup do a deep copy could solve that problem. My gut feeling is that more development on shallow vs. deep copy in D has been needed for a long time.
Jun 24 2007