tdeepcopy.nim 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. discard """
  2. cmd: "nim c --gc:arc --deepcopy:on $file"
  3. output: '''13 abc
  4. 13 abc
  5. 13 abc
  6. 13 abc
  7. 13 abc
  8. 13 abc
  9. 13 abc
  10. 13 abc
  11. 13 abc
  12. 13 abc
  13. 13 abc
  14. called deepCopy for int
  15. called deepCopy for int
  16. called deepCopy for int
  17. called deepCopy for int
  18. called deepCopy for int
  19. called deepCopy for int
  20. called deepCopy for int
  21. called deepCopy for int
  22. called deepCopy for int
  23. called deepCopy for int
  24. called deepCopy for int
  25. 0'''
  26. """
  27. type
  28. PBinaryTree = ref object of RootObj
  29. le, ri: PBinaryTree
  30. value: int
  31. proc mainB =
  32. var x: PBinaryTree
  33. deepCopy(x, PBinaryTree(ri: PBinaryTree(le: PBinaryTree(value: 13))))
  34. var y: string
  35. deepCopy y, "abc"
  36. echo x.ri.le.value, " ", y
  37. for i in 0..10:
  38. mainB()
  39. type
  40. Bar[T] = object
  41. x: T
  42. proc `=deepCopy`[T](b: ref Bar[T]): ref Bar[T] =
  43. result.new
  44. result.x = b.x
  45. when T is int:
  46. echo "called deepCopy for int"
  47. else:
  48. echo "called deepCopy for something else"
  49. proc main =
  50. var dummy, c: ref Bar[int]
  51. new(dummy)
  52. dummy.x = 44
  53. deepCopy c, dummy
  54. for i in 0..10:
  55. main()
  56. echo getOccupiedMem()