> i would deprecate finalizers

I may agree, but i think automatic calling of destructors for references does 
only work with devel compiler and --gc:arc? At least this test was only working 
with arc some weeks ago:

[https://forum.nim-lang.org/t/5786](https://forum.nim-lang.org/t/5786)

So code will be incompatible for old/new Nim.

I did just this test with latest devel compiler:
    
    
    type
      Object* = ref object of RootRef
        impl*: pointer
    
    type
      InitiallyUnowned* = ref object of Object
    
    type
      Widget* = ref object of InitiallyUnowned
    
    proc finalizeGObject*[T](o: ref T) =
      echo "finalizeGObject"
    
    proc finalizeDiscard*[T](o: ref T) =
      echo "discard"
    
    proc main =
      var w: Widget
      #new(w, finalizeDiscard)
      new(w)
      echo "OK"
      new(w, finalizeGObject)
      echo "OK"
    
    main()
    
    
    Run
    
    
    $ nim c --gc:arc -r t.nim
    
    Hint: gcc   -o /tmp/hhh/t  
/tmp/stefan/.cache/nim/t_d/stdlib_allocators.nim.c.o 
/tmp/stefan/.cache/nim/t_d/stdlib_io.nim.c.o 
/tmp/stefan/.cache/nim/t_d/stdlib_system.nim.c.o 
/tmp/stefan/.cache/nim/t_d/@mt.nim.c.o    -ldl [Link]
    Hint: 20053 LOC; 0.147 sec; 24.605MiB peakmem; Debug build; proj: 
/tmp/hhh/t.nim; out: /tmp/hhh/t [SuccessX]
    Hint: /tmp/hhh/t  [Exec]
    
    OK
    finalizeGObject
    OK
    finalizeGObject
    
    
    
    Run

So interleaved calls of new() with and without finalizer compiles now, which 
may be not really intended by user. To ensure that a finalizer is not called 
unintentionally one may pass a dummy one for each call as finalizeDiscard() 
above, that we will get a compiler error abound rebinding. I think I will do 
that for gintro, without danger of calling active finalizer unintentionally is 
too high. Later I may indeed use destructors, that may be even easier for code 
generation, as for finalizers we have to always use full qualification with 
module prefix matching to object, like new(w, gtk.finalizeGObject) when w is a 
gtk.Widget.

The good news is, that gintro compiles already with ARC and that at least some 
examples work. And testing is really deterministic now, no need for 
GC_fullcollect() and hoping that GC may be not sleeping still :-)

Reply via email to