In 0.18 with --newruntime or in devel, follow code:
type
Object* = object
value: int
proc `=destroy`(x: var Object) =
echo "destroy"
x.value = 0
proc create(value: int): Object =
result.value = value
var obj = create(123)
echo obj.value
output is:
destroy
0
But I excepted:
123
destroy
Did I miss anything?
Yes, I guess you miss the main proc:
type
Object* = object
value: int
proc `=destroy`(x: var Object) =
echo "destroy"
x.value = 0
proc create(value: int): Object =
result.value = value
proc main =
var obj = create(123)
echo obj.value
main()
$ ./t
123
destroy
Destructors work best with objects on the stack, while global variables may live in BSS segment.