Minimal example:
type
ObjKind = enum
A
B
C
Obj = ref object
case kind:ObjKind:
of A: s: string
of B: i: int
of C: a: seq[Obj]
let VEMPTYARR = Obj(kind: C, a: @[])
func `$`(o: Obj): string =
case o.kind:
of A: "A(" & o.s & ")"
of B: "B(" & $o.i & ")"
of C: "C(" & $o.a & ")"
when isMainModule:
var x = 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: A, s: "hello"))
z.a.add(Obj(kind: A, s: "world"))
echo $(z)
Run
The result is:
C(@[B(1), B(2)])
C(@[B(1), B(2), A(hello), A(world)])
Run
Now, I look at the generated code and clearly see 2 `eqcopy__` statements for
each of the assignments (`x` and `z`).
But apparently, `.add` changes `a` internally, meaning the `.a` field of
VEMPTYARR...
When I re-compile with `nim c -d:danger --mm:orc` adding `--deepCopy:on` and
make the assignment with `deepCopy()`, it does work, but then it becomes ultra
slow.
So, what is going on? The `=` leads to a copy, which copies... what exactly?