Say I have the following:
type
Vao = object
glId: cint
What is the best way to ensure that the resource is released? I can think of 3 ideas:
I would prefer not to use experimental features, but I can if it's really the best way. However, I likely will have these objects on the heap, so the most important distinction is: which of the first 2 is more idiomatic Nim?
If there is a standard lifecycle for your resource, e.g. can be disposed after the request or easy wrapped in a try .. defer block, then I feel manual cleanup is usually the better approach. It's more deterministic and deliberate.
If the lifecycle is more ad-hoc, less deterministic, then finalizers are usually the better option so you don't have to worry about forgetting to release/free the resource.
- there is also manual cleanup proc with defer
let vao = createVao()
defer: vao.delete
- don't clean up at all. Resources are freed at program exit anyway.
There is no best way. There are only advantages and disadvantages. Choose wisely depending on your use case. We don't know your use case, so we don't know.