Thread overview
.idup flawed?
Jun 21, 2007
Xinok
Jun 21, 2007
Walter Bright
Jun 24, 2007
Jason House
June 21, 2007
.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.
June 21, 2007
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!
June 24, 2007
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.