This looks like an interesting learning exercise, but in what situations is 
this useful? The example you provided should have a correct =destroy generated 
for SimpleObj and TestObj without you writing one manually.

You could confirm this by making name and otherString a custom type with an 
=destroy proc that prints. I think you can use the --expandArc to see the hooks 
generated for you (see: <https://nim-lang.org/docs/nimc.html>)

The docs mention how =destroy is generated: 
<https://nim-lang.org/docs/destructors.html#hook-lifting>

> ... **likewise for =sink and =destroy.** ... Other value-based **compound 
> types like object** and array are handled correspondingly

In other words, each field has =destroy called on it, i.e. it works like a C++ 
destructor (unless it is manually written)

> What could be done to make the code more easy to use and/or maintain?

If I understand your code correctly, you could implement what you're doing with 
a template and 
[fieldPairs](https://nim-lang.org/docs/iterators.html#fieldPairs) rather than 
macro in this situation (overloading is quite a powerful feature), i.e.
    
    
    type
      Bar = object
        y: int
      
      Foo = object
        bar: Bar
    
    template genDestroy[T]() =
      proc `=destroy`*(x: T) {.inject.} =
        echo "myDestroy: ", $T
        for name, value in x.fieldPairs:
          echo "destroying: ", name
          when value is object:
            value.`=destroy`
    
    genDestroy[Bar]()
    genDestroy[Foo]()
    
    var x = Foo()
    echo x
    
    
    Run

Reply via email to