Hi. Why a local module can't be found at macro-time? myModyle.nim:
echo "myModule loaded"
main.nim:
macro loadModule(): typed =
result = parseStmt("import myModule")
loadModule() # will rise exception "cannot open 'myModule'"
Is it possible to load a local module inside a macros? What I'm doing wrong? Thanks.This can:
import macros
macro loadModule(): typed =
result = quote do:
import mymodule
loadModule()
Heres a macro for importing by string, try it out
Upd. Btw this works for me, could you import your module without macro at all?
import macros
macro loadModule(): typed =
result = parseStmt("import strutils")
loadModule()
This looks like a bug with parseStmt. I think the module resolution is done relative to macros.nim instead of relative to the module which called parseStmt. Here is a version that works and doesn't hard code the module name:
import macros
macro loadModule(module: static[string]): typed =
result = quote do:
import `module`
loadModule "myModule"
Edit: I've submitted it the issue tracker: https://github.com/nim-lang/Nim/issues/7466