tdestructor.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. discard """
  2. output: '''----
  3. myobj constructed
  4. myobj destroyed
  5. ----
  6. mygeneric1 constructed
  7. mygeneric1 destroyed
  8. ----
  9. mygeneric2 constructed
  10. mygeneric2 destroyed
  11. myobj destroyed
  12. ----
  13. mygeneric3 constructed
  14. mygeneric1 destroyed
  15. ----
  16. mydistinctObj constructed
  17. myobj destroyed
  18. mygeneric2 destroyed
  19. ------------------
  20. ----
  21. ----
  22. myobj destroyed
  23. myobj destroyed
  24. myobj destroyed
  25. myobj destroyed
  26. mygeneric1 destroyed
  27. ---
  28. myobj destroyed
  29. myobj destroyed
  30. myobj destroyed
  31. '''
  32. """
  33. type
  34. TMyObj = object
  35. x, y: int
  36. p: pointer
  37. proc `=destroy`(o: var TMyObj) =
  38. if o.p != nil: dealloc o.p
  39. echo "myobj destroyed"
  40. type
  41. TMyGeneric1[T] = object
  42. x: T
  43. TMyGeneric2[A, B] = object
  44. x: A
  45. y: B
  46. proc `=destroy`(o: var TMyGeneric1[int]) =
  47. echo "mygeneric1 destroyed"
  48. proc `=destroy`[A, B](o: var TMyGeneric2[A, B]) =
  49. echo "mygeneric2 destroyed"
  50. type
  51. TMyGeneric3[A, B, C] = object
  52. x: A
  53. y: B
  54. z: C
  55. TDistinctObjX = distinct TMyGeneric3[TMyObj, TMyGeneric2[int, int], int]
  56. TDistinctObj = TDistinctObjX
  57. TObjKind = enum Z, A, B, C, D
  58. TCaseObj = object
  59. z: TMyGeneric3[TMyObj, float, int]
  60. case kind: TObjKind
  61. of Z: discard
  62. of A:
  63. x: TMyGeneric1[int]
  64. of B, C:
  65. y: TMyObj
  66. else:
  67. case innerKind: TObjKind
  68. of Z: discard
  69. of A, B, C:
  70. p: TMyGeneric3[int, float, string]
  71. of D:
  72. q: TMyGeneric3[TMyObj, int, int]
  73. r: string
  74. proc open: TMyObj =
  75. # allow for superfluous ()
  76. result = (TMyObj(x: 1, y: 2, p: alloc(3)))
  77. proc `$`(x: TMyObj): string = $x.y
  78. proc myobj() =
  79. var x = open()
  80. echo "myobj constructed"
  81. proc mygeneric1() =
  82. var x = TMyGeneric1[int](x: 10)
  83. echo "mygeneric1 constructed"
  84. proc mygeneric2[T](val: T) =
  85. var a = open()
  86. var b = TMyGeneric2[int, T](x: 10, y: val)
  87. echo "mygeneric2 constructed"
  88. var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
  89. proc mygeneric3 =
  90. var x = TMyGeneric3[int, string, TMyGeneric1[int]](
  91. x: 10, y: "test", z: TMyGeneric1[int](x: 10))
  92. echo "mygeneric3 constructed"
  93. proc mydistinctObj =
  94. var x = TMyGeneric3[TMyObj, TMyGeneric2[int, int], int](
  95. x: open(), y: TMyGeneric2[int, int](x: 5, y: 15), z: 20)
  96. echo "mydistinctObj constructed"
  97. echo "----"
  98. myobj()
  99. echo "----"
  100. mygeneric1()
  101. echo "----"
  102. mygeneric2[int](10)
  103. echo "----"
  104. mygeneric3()
  105. echo "----"
  106. mydistinctObj()
  107. proc caseobj =
  108. block:
  109. var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
  110. block:
  111. echo "----"
  112. var o2 = TCaseObj(kind: B, y: open())
  113. block:
  114. echo "----"
  115. var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
  116. p: TMyGeneric3[int, float, string](x: 10, y: 1.0, z: "test"))
  117. echo "------------------"
  118. caseobj()
  119. proc caseobj_test_sink: TCaseObj =
  120. # check that lifted sink can destroy case val correctly
  121. result = TCaseObj(kind: D, innerKind: D, r: "test",
  122. q: TMyGeneric3[TMyObj, int, int](x: open(), y: 1, z: 0))
  123. result = TCaseObj(kind: B, y: open())
  124. echo "---"
  125. discard caseobj_test_sink()