tdestructor.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. mygeneric1 destroyed
  17. ----
  18. myobj destroyed
  19. ----
  20. ----
  21. myobj destroyed
  22. '''
  23. cmd: '''nim c --newruntime $file'''
  24. disabled: "true"
  25. """
  26. type
  27. TMyObj = object
  28. x, y: int
  29. p: pointer
  30. TMyGeneric1[T] = object
  31. x: T
  32. TMyGeneric2[A, B] = object
  33. x: A
  34. y: B
  35. TMyGeneric3[A, B, C] = object
  36. x: A
  37. y: B
  38. z: C
  39. TObjKind = enum A, B, C, D
  40. TCaseObj = object
  41. case kind: TObjKind
  42. of A:
  43. x: TMyGeneric1[int]
  44. of B, C:
  45. y: TMyObj
  46. else:
  47. case innerKind: TObjKind
  48. of A, B, C:
  49. p: TMyGeneric3[int, float, string]
  50. of D:
  51. q: TMyGeneric3[TMyObj, int, int]
  52. r: string
  53. proc `=destroy`(o: var TMyObj) =
  54. if o.p != nil: dealloc o.p
  55. echo "myobj destroyed"
  56. proc `=destroy`(o: var TMyGeneric1[int]) =
  57. echo "mygeneric1 destroyed"
  58. proc `=destroy`[A, B](o: var TMyGeneric2[A, B]) =
  59. echo "mygeneric2 destroyed"
  60. proc open: TMyObj =
  61. # allow for superfluous ()
  62. result = (TMyObj(x: 1, y: 2, p: alloc(3)))
  63. proc `$`(x: TMyObj): string = $x.y
  64. proc myobj() =
  65. var x = open()
  66. echo "myobj constructed"
  67. proc mygeneric1() =
  68. var x = TMyGeneric1[int](x: 10)
  69. echo "mygeneric1 constructed"
  70. proc mygeneric2[T](val: T) =
  71. var a = open()
  72. var b = TMyGeneric2[int, T](x: 10, y: val)
  73. echo "mygeneric2 constructed"
  74. var c = TMyGeneric3[int, int, string](x: 10, y: 20, z: "test")
  75. proc mygeneric3 =
  76. var x = TMyGeneric3[int, string, TMyGeneric1[int]](
  77. x: 10, y: "test", z: TMyGeneric1[int](x: 10))
  78. echo "mygeneric3 constructed"
  79. echo "----"
  80. myobj()
  81. echo "----"
  82. mygeneric1()
  83. echo "----"
  84. mygeneric2[int](10)
  85. echo "----"
  86. mygeneric3()
  87. proc caseobj =
  88. block:
  89. echo "----"
  90. var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10))
  91. block:
  92. echo "----"
  93. var o2 = TCaseObj(kind: B, y: open())
  94. block:
  95. echo "----"
  96. var o3 = TCaseObj(kind: D, innerKind: B, r: "test",
  97. p: TMyGeneric3[int, float, string](x: 10, y: 1.0, z: "test"))
  98. block:
  99. echo "----"
  100. var o4 = TCaseObj(kind: D, innerKind: D, r: "test",
  101. q: TMyGeneric3[TMyObj, int, int](x: open(), y: 1, z: 0))
  102. caseobj()