> The reason I like it as it seems to allow one to specify the exact behaviour
> required...
@Araq, by having this capability, it would seem to make deepCopy work for any
type no matter how deeply nested as the nested types would just recursively
deepCopy; shallowCopy would only copy to the first level of fields. In fact,
with the new specification deepCopy seems redundant as if seq was implemented
as the motivating example, all nested copies automatically happen just by
virtue of the assignment of the elements.
However, this seems to break the current implementation of shallowCopy which
perhaps also uses assignment to copy to one level of fields. Since I believe
that these have an equivalent to a pointer to their contents, I expected that a
shallowCopy would just copy this pointer so that a shallowCopy`ed `seq/string
would reflect subsequent changes on the source in the destination, but it seems
to do the same as deepCopy which is the default =copy characteristic:
var seq0 = newSeq[int](1)
var seq1: seq[int]
shallowCopy(seq1, seq0)
seq0[0] = 42
seq1[0].echo # produces 0 not 42 as expected
Run
I would think that shallowCopy now needs to just to a simple byte copy in order
to avoid triggering the deep copy.
Also, a word on expected characteristics of seq; in many other languages, an
array/seq is a "reference" type rather than a "value" type which means that
reference type assignments only have their pointers copied where value
assignments are compete copy assignments. Users coming from those languages
will be surprised by Nim's default deep copy. This means that other than for
var pointers, array/seq/string parameters get deep copied and assignments such
as the above won't be as expected.
The documentation cautions on the semantics of assignment of seq/string and the
use of shallowCopy but doesn't give examples and explicitly state the
difference between those languages and how to get the same behaviour as those
languages when desired.
In a nutshell - shallowCopy needs to be byte copy but also needs to cause the
copy/move semantics.