# main.nim
import macros
macro gen_import(): untyped =
return newNimNode(nnkImportStmt).add infix(
ident("foo"),
"as",
ident("bar")
)
gen_import()
echo bar.foobar
# foo.nim
const foobar* = "foobar"
I got this error:
lib/core/macros.nim(1348, 13) Error: cannot open file: foo
How can I fix this error?
Like this:
import macros
macro gen_import(): untyped =
let module = ident("foo")
let alias = ident("bar")
result = quote do:
import `module` as `alias`
gen_import()
echo bar.foobar
Your original form worked for me for modules in my explicit nim --path from a nim.cfg (like cligen), but not in the implicit PWD path your foo example used. Not sure why.macro gen_import():untyped =
quote do:
import foo as bar
works, not sure how/why sorry