this is correct. when implementing a function table you should prefer
{.nimcall.} to avoid the extra overhead of {.closure.}
while i don't know the rest of your logic, so maybe there's a reason you've
structured things that way, but perhaps you might like to consider as an
alternative:
type actionKind = enum akIdle,akLook
type Mob* = object
name*: string
action:actionKind
actions: array[actionKind,proc(m:var Mob)]
proc action_idle(m:var Mob)
proc action_look(m:var Mob)
proc initMob():Mob = Mob(actions: [akIdle: action_idle, akLook:
action_look])
proc act(m:var Mob) = m.actions[m.action](m)
proc action_idle( m:var Mob) =
echo "idle, now looking"
m.action = akLook
proc action_look( m:var Mob) =
echo "looking, time to rest"
m.action = akIdle
Run
benefits as i see them: no global state, no duplicating [idle:(idle:...].
adding a new action does still require you to a)update actionKind, b) add the
new proc c)add it to initMob which could be errorprone, but this is what macros
are for