Hi. Does Nim supports anything like a "dynamic loading of packages"? For example something like a program:
import pack0
init(1)
echo someProc(5) #must display 6
init(2)
echo someProc("qwe") #must display qwerty
and 3 packages with some logic. pack0.nim ("proxy" package) contains:
var dstBack = 0 # some variable-indetifier (will indicate, from wich package someProc will be called)
macro init*(back:int) = # some initialization macros, who sets variable's value before "someProc" was called
dstBack = back
macro someProc* = # some macros, who will return "someProc" from destination package (in choice of "dstBack")
case: dstBack
of 1:
#check that pack1 is available...
#if true:
return pack1.someProc
#else show compiler's exception with an due message
of 2:
#check that pack1 is available...
#if true:
return pack2.someProc
#else show compiler's exception with an due message
else:
discard
return proc () = discard #return empty function
nim due package must be accessed only when dstBack contains certain value (1 or 2). pack1.nim:
proc someProc*(a:int): int =
result a += 1
pack2.nim:
proc someProc*(a:string): string =
result a & "rty"
Is it possible to realize this scheme in Nim? Thanks.well, I'm found similar theme. This works:
import macros
macro load*(modulesSeq: varargs[untyped]): untyped =
result = newStmtList()
for module in modulesSeq:
result.add parseStmt("from " & $module & " import nil")
const a = 0
when a == 0:
load strutils
echo strutils.join(@[1,2,3], ", ")
elif a == 2:
load asdsadasd # will excepted if set a to 2
cool