There should be nothing special for this case, if the macro is used in the same
module, where defined. You just need to use `import` before calling the macro
(but can use after the macro definition itself).
If the proc is defined in one module (_a_), the macro in another (_b_) and is
called in yet another (_c_), then you need either:
* `import a` in _c_, prior to calling the macro; no need then to import _a_
in _b_, the macro will use an unbound identifier; so you can really then have
in the same program some another module (_a2_) defining a proc with same name
and imported, together with _b_, in yet another module (_c2_); so, in _c_ a
proc from _a_ will be called, and in _c2_ \- from _a2_, both via the same macro
from _b_.
* you can bind the identifier used in macro (for the proc) to the actual
symbol (to the proc from _a_), using `newCall(bindSym"theProcFromA")` instead
of `newCall("theProcFromA")` (what Araq suggested), so what imported in _c_ (or
where the macro is used) doesn't matter; _a_ should be imported in _b_ then
before the macro's definition.
* in templates or quote do: in macros by default identifier is bound if the
symbol is in scope of the macro definition (i.e. will refer to the same proc),
and unbound otherwise; you can force binding (as with `bindSym`) by using
`bind` statement (then you'll get a compile-time error, if the proc is not in
the scope, instead of late binding), or force letting it unbound by `mixin`
statement; in `quote do:` you can also just use backticks around identifiers
for explicit binding (you might need to declare the proc with as _procvar_
(say, via `{.procvar.}`) for this to work).
E.g.:
# module a
proc p* = echo "p of a"
proc pv* {.procvar.} = echo "pv of a"
#module a2
proc p* = echo "p of a2"
# module b
import a
import macros
macro x1*: untyped =
result = newNimNode(nnkStmtList)
result.add newCall(bindSym"p")
# the same
macro x2*: untyped =
result = quote do:
bind p
p()
# yet the same, but ``pv`` should be a ``procvar``
macro x3*: untyped =
result = quote do:
`pv`()
# all ``x1``, ``x2`` and ``x3`` will call ``a.p``/``a.pv``, wherever called
# ``p`` is always unbound
macro y1*: untyped =
result = newNimNode(nnkStmtList)
result.add newCall("p")
# ``p`` is bound, just because *a* is imported, would be unbound otherwise
macro y2*: untyped =
result = quote do:
p()
# ``p`` is implicitly unbound
template y3* =
mixin p
p()
# module c
# the main module for the example
import b
x1() # -> p of a
x2() # -> p of a
x3() # -> p of a
import a
y1() # -> p of a
y2() # -> p of a
y3() # -> p of a
import c2
# module c2
import b
x1() # -> p of a
x2() # -> p of a
x3() # -> pv of a
import a2
y1() # -> p of a2
y2() # -> p of a
#y3() # won't compile, both ``p`` from *a* and *a2* match