Hi I am new to this language so please bear with me. I was trying to create a simple example with refs and destructors.
Here is the code:
type
MyObj = object of RootObj
name: string
MyObjRef = ref MyObj
proc `=destroy`(obj: var MyObj) =
echo "Destructor called for ", obj.name
#[
proc finalizer(obj: MyObjRef) =
echo "Finalizer called for ", obj.name
]#
proc newMyObj(name: string): MyObjRef =
let r = new(MyObjRef)
r.name = name
return r
#[
new result, finalizer
result.name = name
]#
proc myfunc() =
var o1 = newMyObj("Obj1")
var o2 = MyObj(name: "Obj2")
echo o1.name, " is ready"
echo o2.name, " is ready"
when isMainModule:
myfunc()
I have also tried with finalizer (see commented code), but no matter what I do, the result I get is:
Obj1 is ready
Obj2 is ready
Destructor called for Obj2
I have tried both with and without '--newruntime' flag, but the results are the same. The destructor (or finalizer) is not called for reference object. I even tried calling GC_fullCollect(), but did not work. What am I doing wrong?
I am running compiler version 1.2.0 on Linux