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