type
O1 = object of RootObj
i: int
R1 = ref O1
proc `=destroy`(x: var O1) =
echo "destroy"
type
O2 = object of O1
j: int
R2 = ref O2
proc `=destroy`(x: var O2) =
`=destroy`(O1(x))
proc main =
var r = R2(i: 7, j: 3)
echo r.i, r.j
main()
Run
$ nim c --gc:arc -r t.nim
73
destroy
Run
This seems to work fine. Related to
[https://forum.nim-lang.org/t/5825#36241](https://forum.nim-lang.org/t/5825#36241)
I have the feeling that using these =destroy finalizers is even simpler than
specifying finalizers for each new() call, and when user subclass a widget,
they have to type only proc `=destroy`(x: var O2) = `=destroy`(O1(x)) for their
subclass to make finalizers work for subclass. That is my guess, will not use
it now as arc is not yet the default.
Question: is proc `=destroy`(x: var O2) = `=destroy`(O1(x)) the best way to
call destructor of parent class? So we have to know the name of parent class,
but I think this is never a real problem. But maybe there is still a better
method to call direct parent? And I may want to have only ref types in use, as
the objects are never directly used. Exception is the =destroy proc, which
needs a object type. May something like proc `=destroy`(x: var R2[]) =
`=destroy`(R1(x)) be possible?