removes references to this in the constructor section (#22730)

This commit is contained in:
Juan M Gómez
2023-09-20 11:02:55 +01:00
committed by GitHub
parent fefde3a735
commit af617be67a

View File

@@ -2341,11 +2341,10 @@ type Foo* = object
x: int32
proc makeFoo(x: int32): Foo {.constructor.} =
this.x = x
result.x = x
```
It forward declares the constructor in the type definition. When the constructor has parameters, it also generates a default constructor.
Notice, inside the body of the constructor one has access to `this` which is of the type `ptr Foo`. No `result` variable is available.
Like `virtual`, `constructor` also supports a syntax that allows to express C++ constraints.
@@ -2371,11 +2370,11 @@ type
NimClass* = object of CppClass
proc makeNimClass(x: int32): NimClass {.constructor:"NimClass('1 #1) : CppClass(0, #1)".} =
this.x = x
result.x = x
# Optional: define the default constructor explicitly
proc makeCppClass(): NimClass {.constructor: "NimClass() : CppClass(0, 0)".} =
this.x = 1
result.x = 1
```
In the example above `CppClass` has a deleted default constructor. Notice how by using the constructor syntax, one can call the appropiate constructor.