If I have something like:
type Timer = object
time: float = 0
duration: float
And I want to destroy the timer when time > duration, how would I do that?
Most of time Nim's ARC or ORC is used in most of codes objects are destroyed automatically as soon as they are no longer needed.
So You don't need to destroy them manually
Stack Objects are will be destroyed automatically via a =destroy hook when the procedure returns.
Heap Objects (ref object) it is destroyed when the last reference to it is removed settingvvalue holding to nil.
type Timer = object
time: float
duration: float
var timers: seq[Timer] = @[Timer(time: 0, duration: 5.0)]
# To destroy expired one:
for i in countdown(timers.high, 0):
if timers[i].time > timers[i].duration:
timers.delete(i) # This destroy the object ARC then calls =destroy no need to do it manually.
You can also see when it get destroyed:
proc `=destroy`(x: var Timer) =
# This runs automatically
echo "Timer resources being cleaned ..."
Only If you are using raw pointers (like ptr Timer) and manual allocation via alloc, you must use dealloc to free the memory
proc drop[T](obj: sink T) =
discard
Unless you're doing manual memory management, you don't need to do anything to destroy objects in nim. They are destroyed automatically when they go out of scope (no longer reachable from the stack).
In your case, you probably have some collection of timers you iterate over. All you need to do is to remove the unwanted timers from the collection.