-1

Within a file i have several imports from the same directory. If i change the location of this file, rather than having to add one to one '../' inside import, i'd like to use a template to build them and make my life easier when it comes to change paths.

I'd like to know if i could achieve this objective with templates. This is an example of what i expect to get:

template importRoot(p: untyped) ???
importRoot a/b/c.nim # Resolves to import full/path/a/b/c.nim
importRoot a/a.nim   # Resolves to import full/path/a/a.nim
Arrrrrrr
  • 802
  • 6
  • 13

1 Answers1

1

You'll need a macro. For example something along the following lines:

import macros

const root = "rootfolder"

macro importRoot*(paths: varargs[untyped]): untyped =
  result = newNimNode(nnkStmtList)
  let sub = !root
  for p in paths:
    add result, quote do:
      import `sub`.`p`

Note that it may be easier to simply add a --path option on the command line instead.

Reimer Behrends
  • 8,600
  • 15
  • 19
  • Thank you. I have a lot of modules and folders, i dont think path would help here, but this macro can ease a bit my pain. – Arrrrrrr Nov 04 '15 at 08:38