The problem is not only about freeing memory but releasing the resources to the 
pool of resources (OS, database, etc.).

Let say I have a bit of code 
    
    
    proc getIter(p: Params): iterator: int =
      iterator iterWithResources: int {.closure.} =
      ...
      result = iterWithResources
      
      ...
      let iter = getIter(someParams)
      for i in iter():
      ...
    
    
    Run

If I understand correctly the [Nim Destructors and Move 
Semantics](https://nim-lang.org/docs/destructors.html) documentation, I would 
need to write a destructor proc for the type of the variable `iter`. What is 
its type?

The compiler error message explicitly shows that `=destroy` proc only apply to 
`object` types. So how do you define a destructor (or a resource collection 
proc) for an iterator?

And is there a way, in that destructor, to know if the iterator completed 
successfully or if it is still alive? If the iterator completed, normally the 
resources have been released: there is no need to do something in the 
destructor. But if it did not complete and is still waiting for the next call, 
then the programmer has to free the resources.

Reply via email to