You can force a deepcopy of one specific ref object with deepCopy. Looking at a 
RosettaCode example <https://rosettacode.org/wiki/Deepcopy#Nim> I found that 
you could do (on a smaller minimal example):
    
    
    type
        ObjKind = enum
            B
            C
        
        Obj = ref object
            case kind:ObjKind:
                of B: i: int
                of C: a: seq[Obj]
    
    let vemptyarr = Obj(kind: C, a: @[])
    
    func `$`(o: Obj): string =
        case o.kind:
            of B: "B(" & $o.i & ")"
            of C: "C(" & $o.a & ")"
    
    var x:Obj
    x.deepCopy vemptyarr
    x.a.add(Obj(kind: B, i: 1))
    x.a.add(Obj(kind: B, i: 2))
    echo $(x)
    
    var z = vemptyarr
    z.a.add(Obj(kind: B, i: 3))
    z.a.add(Obj(kind: B, i: 4))
    echo $(z)
    
    
    Run

It does slow down quite a lot. I am curious of the internal representation of 
the object. You have both a recursive type with a case of field. You ask Nim to 
copy sequences of sequences of sequences ...

I don't know how the memory layout of recursive sequence types looks. How do 
you copy it efficiently ? Since sequences are nested, we don't know without 
exploring the full tree, the exact size of the object.

Reply via email to