Hi,
Recently I realized that shallowCopy doesn't create a real link like in Python (see https://github.com/nim-lang/Nim/issues/3409 ). Namely, if we add one element to a sequence, then the shallowed copy is either modified or not (depending on whether reallocation is needed).
There is a function called shallow(s: var seq[T]) which sets a bit (cleverly hidden in reserved's highest bit position). If this bit is set to 1 for sequence x, then each of the var y = x copy is a shallow copy. This shallow(...) function is not very used, I could locate two calls from Nim's std lib.
So instead of this unpredictable connection between the two objects, I suggest something different. Let's use this bit to indicate whether the seq is read-only. Let me show an example:
var x = @[1,2,3]
x.freeze() # this would set the bit to 1
var y = x # this can be a shallowCopy because x can't be modified
x[0] = 99 # this reallocates x, copies the values, set x[0]=99 in the new memory location
echo y # prints: @[1,2,3]
y[0] = 5 # this reallocates y, copies the values, set y[0]=5 in the new memory location
What do you think? As motivation: we could write a function which takes a seq and returns an iterator to that sequence without copy it immediately.
I don't have a proposal whether freeze() could be called on a not var seq.