# Destructors vs finalizers

I curiously followed the discussion about 
[destructors](https://forum.nim-lang.org/t/3254) in the last weeks and I 
realised the division in resource management of stack and heap based variables. 
No other language I know has this kind of seperation.

Moreover I found a [thread from 2013](https://forum.nim-lang.org/t/326), which 
destribes finalizers as "hacked together" and "deprecated" Could you please 
explain to me, why we still have this then and the compiler wont complain at 
all?

I think unifiing resource management in our code is as important as the 
implementation details described in the other [froum 
post](https://forum.nim-lang.org/t/3254) and we should come to a conclusion, 
before Nim 1.0 since otherwise I highly doubt many people would consider Nim to 
be ready.

Furthermore I can't think of a single instance, where the resources managed by 
a type should get disposed in a diffrent way, depending of the types memory 
location. Therefore you could use the following code as a workaround but this 
looks kinda hackish.
    
    
    {.experimental.}
    type
        DestrcTest = object
            a:int
    
    var count = 0
    proc `=destroy`(test: DestrcTest) = count += test.a
    
    proc test() =
        for _ in 0..<100:
            var a =  DestrcTest(a:1)
            var b : ref DestrcTest
            new(b, proc (t: ref DestrcTest) = `=destroy`(t))
            b.a = 1
    
    test()
    GC_fullCollect()
    echo count
    

* * *

# Two competing desturctor definitions

Another issue is that at the moment you can create a destructs in at least two 
diffrent ways. 
    
    
    {.experimental.}
    type
        DestrcTest = object
            a:int
    
    var count = 0
    proc `=destroy`(test:DestrcTest) = count += test.a * 10
    proc `destroy`(test: DestrcTest) {.destructor.} = count += test.a
    
    proc test() =
        for _ in 0..<100:
            var a =  DestrcTest(a:1)
    
    test()
    echo count
    

And which of them shall have the higher precedence? By this example you can 
tell that the function `destroy` with the pragma annotation got used, but if 
you just switch the two destructor definitions the other gets used, which isn't 
quite acceptable.

Reply via email to