You can PR that in ;)
Awesome, I’ll be watching the repo!
For posterity, I also made a version that supports any number of hooks
registered under one name, based on the compile-time table version.
import tables, macros
var HOOKS {.compileTime.}: Table[string, seq[NimNode]]
macro add_hoo
I created a cookbook repo to store some recipes, I had (I have plenty sprayed
over my repos!).
Yours is the first one:
[https://github.com/status-im/nim-cookbook/blob/264cc90d/macros_register_hooks.nim](https://github.com/status-im/nim-cookbook/blob/264cc90d/macros_register_hooks.nim)
the identifiers doesn't need to be valid/existing but it prevents passing a
whole statement or parenthesis or brackets. It's really optional, just an extra
check.
For some reason the "parameter constraints" section of what was once in the Nim
manual was moved to the experimental features manual along with term rewriting
macros, even though it is an independent feature from them.
[Here](https://nim-lang.org/docs/manual_experimental.html#term-rewriting-macr
Thanks mratsim, the use of `{.compileTime.}` was what I was missing :)
One follow-up question: what does the `untyped{ident}` syntax mean? Is it
restricting the argument to valid identifiers?
Thanks
And second version, with run_hook as pragma
import tables, macros
var hooks {.compileTime.}: Table[string, NimNode]
macro registerHook(name: untyped{ident}, body: untyped): untyped =
result = newStmtList()
result.add newProc(
name = name,
Version 1, exactly what you asked.
import tables, macros
var hooks {.compileTime.}: Table[string, NimNode]
macro registerHook(name: untyped{ident}, body: untyped): untyped =
result = newStmtList()
result.add newProc(
name = name,
body = bo
Hi there,
I want to write macros that takes code written in one place and inserts it
elsewhere. In use this would look something like:
registerHook post_foo:
echo “foo done. x=“, x
...
proc foo =
let x = 10
runHooks post_foo
Run
My