var module = "jester"
import module
If not, how do I implement something like this?IMO, Nim can't import module at run time. You must specify the name of module directly.
import "jester"
If you want to import module at run time, please write Nim code like this:).
var temp = newFileStream(tempFile, fmWrite)
if not isNil(temp):
temp.writeLine("import timeit")
if def == "":
temp.writeLine("""var m = monit("test-whole")""")
temp.writeLine("m.start()")
please write Nim code like this
Maybe I missed a lot, but I have no idea what that part of your answer may mean and how it may help for the initial question.
I mean if you want to import module dynaically, yon can write it directly into file or use when flag. for example,
import streams, strformat
let
module = readline(stdin)
tempFile = getTempDir() & "timeit_temp.nim"
var temp = newFileStream(tempFile, fmWrite)
temp.writeLine(fmt"import {module}")
temp.write(othercode)
dicscard execCmdEx(fmt"nim c -r -f --d:{option} --verbosity=0 --hints=off --hint[source]=on " & tempFile)
or
when defined(this):
import this
elif defined(that):
import that
const some_module {.strdefine.} = "some_module"
template makeAnImport(module: static[string]): untyped = import module
makeAnImport(some_module)
:)
I think there's some misunderstanding here. The short answer to "is this possible?" is no, this is not possible. Since nim is a compiled language, all imported modules have to be compiled and embedded in the resulting binary. It is possible in dynamic languages that offer eval of some sort, but not in statically compiled languages.
Now in theory it is possible to make a lib that would allow eval and dynamic imports in some form, it would embed nim compiler in it, and maybe utilize hot code reloading, but i assure you, that's not something you want to consider at this point.