tdeepcopy.nim 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. discard """
  2. output: '''
  3. 13 abc
  4. called deepCopy for int
  5. called deepCopy for int
  6. done999 999
  7. '''
  8. """
  9. import threadpool
  10. block one:
  11. type
  12. PBinaryTree = ref object
  13. le, ri: PBinaryTree
  14. value: int
  15. proc main =
  16. var x: PBinaryTree
  17. deepCopy(x, PBinaryTree(ri: PBinaryTree(le: PBinaryTree(value: 13))))
  18. var y: string
  19. deepCopy y, "abc"
  20. echo x.ri.le.value, " ", y
  21. main()
  22. block two:
  23. type
  24. Bar[T] = object
  25. x: T
  26. proc `=deepCopy`[T](b: ref Bar[T]): ref Bar[T] =
  27. result.new
  28. result.x = b.x
  29. when T is int:
  30. echo "called deepCopy for int"
  31. else:
  32. echo "called deepCopy for something else"
  33. proc foo(b: ref Bar[int]): int = 999
  34. # test that the disjoint checker deals with 'a = spawn f(); g = spawn f()':
  35. proc main =
  36. var dummy: ref Bar[int]
  37. new(dummy)
  38. dummy.x = 44
  39. #parallel:
  40. let f = spawn foo(dummy)
  41. let b = spawn foo(dummy)
  42. echo "done", ^f, " ", ^b
  43. main()