following up my event system for an ECS (from thread: https://forum.nim-lang.org/t/8784 )
i now have a different error.
My goal is to enque a callback. The reason i want to do this is when a loop iterates over components a callback that is immediately triggered, causes the table to have a different size. And therefore the table asserts triggers.
So what i want to do is to store closures in a deque and call them later when its save. The closure should then call its bound inner closure:
import intsets, hashes, std/deques, tables
type
Registry* = ref object
ev: Table[Hash, IntSet] ## container for events
evs: Deque[proc ()] ## the storage for triggerLater closures
proc triggerLater*(reg: Registry, ev: auto) =
## Enqeues an event to be triggered on reg.update()
const typehash = hash($ev.type)
if not reg.ev.hasKey(typehash): return
type innter = proc (ev: ev.type) {.nimcall.}
for pcb in reg.ev[typehash]:
proc clos() {.closure.} =
cast[innter](pcb)(ev)
addLast(reg.evs, clos)
type MyEv = object
var reg = Registry()
var myev = MyEv()
reg.triggerLater(myev)
error:
: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkClosure' [FieldDefect]