45.12 ShallowCopy

ShallowCopy( obj )

ShallowCopy returns a copy of the object obj. You may apply ShallowCopy to objects of any type, but for objects that are not lists or records ShallowCopy simply returns the object itself.

For lists and records the result is a new list or record that is not identical to any other list or record (see Identical Lists and Identical Records). This means that you may modify this copy new by assignments (see List Assignment and Record Assignment) or by adding elements to it (see Add and Append), without modifying the original object obj.

    gap> list1 := [ 1, 2, 3 ];;
    gap> list2 := ShallowCopy( list1 );
    [ 1, 2, 3 ]
    gap> list2[1] := 0;;  list2;
    [ 0, 2, 3 ]
    gap> list1;
    [ 1, 2, 3 ] 

That ShallowCopy returns the object itself if it is not a list or a record is consistent with this definition, since there is no way to change the original object obj by modifying new, because in fact there is no way to change the object new.

ShallowCopy basically executes the following code for lists, and similar code for records.

    new := [];
    for i  in [1..Length(obj)]  do
        if IsBound(obj[i])  then
            new[i] := obj[i];
        fi;
    od; 

Note that ShallowCopy only copies the top level. The subobjects of the new object new are identical to the corresponding subobjects of the object obj. If you want to copy recursively use Copy (see Copy).

Previous Up Top Next
Index

GAP 3.4.4
April 1997