mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-07 12:24:19 +00:00
removes references to this in the constructor section (#22730)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user