proc newAuthMessage() =
...
proc newSubscribeMessage() =
...
and I want to call these procedures dynamically, so we have string "Auth" , "Subscribe", and now I need concatenate the string and call them. I tried with template but no luck.
import macros
import tables
var nameToProc {.compileTime.} = newTable[string, proc(): string {.nimcall.}]()
macro registerProc(p: untyped): untyped =
result = newTree(nnkStmtList, p)
echo repr(p[0])
let procName = p[0]
let procNameAsStr = $p[0]
result.add quote do:
nameToProc.add(`procNameAsStr`, `procName`)
proc foo: string {.registerProc.} = "foo"
proc bar: string {.registerProc.} = "bar"
proc baz: string {.registerProc.} = "baz"
doAssert nameToProc["foo"]() == "foo"
But I get the following error:
Error: unhandled exception: key not found: foo [KeyError]
This is my solution to call functions by name: https://github.com/krux02/opengl-sandbox/blob/master/examples/console.nim
This example in this project does not have any dependentcies of that project itself, it should just work stand alone.