help with copying mutable objects

So, I have a map of records.
And I want to make a complete copy of it.

My experiences here tell me that simply saying B=A would not be sufficient.

Taking it down a step I could do foreach i in A B=A, which I really want to work, but something tells me I'd be accomplishing just as little, because of my complete lack of comprehension of how = works in OO languages. Is the only way to copy this data structure to go through and copy each field of each element? Or is there some keen operator to copy over the actual values and not just reference the object itself?
 

Veracity

Developer
Staff member
(This really has nothing to do with OO languages; C does the same thing when you use "=" from one struct pointer to another. The thing you have to understand, if you want to compare ASH to C is that every record or map variable in your ASH program is, conceptually, a pointer to the record or map.

You want some sort of "clone" operator. And even there, you might want a "shallow" or a "deep" clone: do you simply make a new object and copy (with =) all the fields, or do you make a new object and recursively clone the fields?

In your example, if you want to create a new map and add all the existing records - not cloned copies - to the new map, do this:
Code:
foreach key, val in A
    B[key] = val;

If you want to make a new map and clone the records too, you will need to do
Code:
foreach key, val in A
    B[key] = my_clone( val );
...and you would have to write my_clone yourself.
 
Wherein myclone is essentially
Code:
type myClone(type A){
 type T;
 T.field1=A.field1;
 T.field2=A.field2;
 . . .
 return T;
}
?

This will work I suppose. Thank you.
 
Last edited:
Top