I tried to write the code (by following the manual):
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"
Run
But I get the following error:
Error: unhandled exception: key not found: foo [KeyError]
Run