I frequently have the problem that I want to take immutable clones of e.g. sequences. For instance:
var a = @[1,2,3]
let b = a # want a clone here ...
a.setLen(0)
# ... so that b remains @[1,2,3]
I solve the problem by taking a deepCopy. But deepCopy has a very weird interface (requires a var) resulting in rather ugly code, because it requires a temporary variable and a type repetition:
var a = @[1,2,3]
var tmp: seq[int]
deepCopy(tmp, a)
let b = tmp
Am I missing something here? Every time I write such code I begin searching the standard library, because I can't believe that there isn't any better solution for this. Basically I just want to write:
let b = a.clone
# or
let b = a.deepCopy
But so far I haven't found anything like this. I'm wondering why this is the case? Wouldn't it be possible to just add:
proc deepCopy[T](x: T): T {.inline.} =
var tmp: T
deepCopy(tmp, x)
tmp
Is there any reason why this does not exist? Would it be okay to make a PR for this? Should it be named deepCopy or more commonly clone?