tconstructor.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. discard """
  2. targets: "cpp"
  3. cmd: "nim cpp $file"
  4. output: '''
  5. 1
  6. 0
  7. 123
  8. 0
  9. 123
  10. ___
  11. 0
  12. 777
  13. 10
  14. 123
  15. 0
  16. 777
  17. 10
  18. 123
  19. ()
  20. '''
  21. """
  22. {.emit:"""/*TYPESECTION*/
  23. struct CppClass {
  24. int x;
  25. int y;
  26. CppClass(int inX, int inY) {
  27. this->x = inX;
  28. this->y = inY;
  29. }
  30. //CppClass() = default;
  31. };
  32. """.}
  33. type CppClass* {.importcpp, inheritable.} = object
  34. x: int32
  35. y: int32
  36. proc makeCppClass(x, y: int32): CppClass {.importcpp: "CppClass(@)", constructor.}
  37. #test globals are init with the constructor call
  38. var shouldCompile {.used.} = makeCppClass(1, 2)
  39. proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
  40. #creation
  41. type NimClassNoNarent* = object
  42. x: int32
  43. proc makeNimClassNoParent(x:int32): NimClassNoNarent {. constructor.} =
  44. result.x = x
  45. discard
  46. let nimClassNoParent = makeNimClassNoParent(1)
  47. echo nimClassNoParent.x #acess to this just fine. Notice the field will appear last because we are dealing with constructor calls here
  48. var nimClassNoParentDef {.used.}: NimClassNoNarent #test has a default constructor.
  49. #inheritance
  50. type NimClass* = object of CppClass
  51. proc makeNimClass(x:int32): NimClass {. constructor:"NimClass('1 #1) : CppClass(0, #1) ".} =
  52. result.x = x
  53. #optinially define the default constructor so we get rid of the cpp warn and we can declare the obj (note: default constructor of 'tyObject_NimClass__apRyyO8cfRsZtsldq1rjKA' is implicitly deleted because base class 'CppClass' has no default constructor)
  54. proc makeCppClass(): NimClass {. constructor: "NimClass() : CppClass(0, 0) ".} =
  55. result.x = 1
  56. let nimClass = makeNimClass(1)
  57. var nimClassDef {.used.}: NimClass #since we explictly defined the default constructor we can declare the obj
  58. #bug: 22662
  59. type
  60. BugClass* = object
  61. x: int # Not initialized
  62. proc makeBugClass(): BugClass {.constructor.} =
  63. discard
  64. proc main =
  65. for i in 0 .. 1:
  66. var n = makeBugClass()
  67. echo n.x
  68. n.x = 123
  69. echo n.x
  70. main()
  71. #bug:
  72. echo "___"
  73. type
  74. NimClassWithDefault = object
  75. x: int
  76. y = 777
  77. case kind: bool = true
  78. of true:
  79. z: int = 10
  80. else: discard
  81. proc makeNimClassWithDefault(): NimClassWithDefault {.constructor.} =
  82. result = NimClassWithDefault()
  83. proc init =
  84. for i in 0 .. 1:
  85. var n = makeNimClassWithDefault()
  86. echo n.x
  87. echo n.y
  88. echo n.z
  89. n.x = 123
  90. echo n.x
  91. init()
  92. #tests that the ctor is not declared with nodecl.
  93. #nodelc also prevents the creation of a default one when another is created.
  94. type Foo {.exportc.} = object
  95. proc makeFoo(): Foo {.used, constructor, nodecl.} = discard
  96. echo $Foo()
  97. type Boo = object
  98. proc `=copy`(dest: var Boo; src: Boo) = discard
  99. proc makeBoo(): Boo {.constructor.} = Boo()
  100. proc makeBoo2(): Boo = Boo()
  101. block:
  102. proc main =
  103. var b = makeBoo()
  104. var b2 = makeBoo2()
  105. main()