Currently, macros.nim does not offer a way to convert a NimNode (obtained from getType on a typed expression) into the corresponding typedesc:
import macros
proc f(T: typedesc[SomeInteger]) =
echo "called f with some integer"
proc f(T: typedesc[string]) =
echo "called f with string"
macro aMacro(x: typed) =
let typeNode = x.getType()
# Call f at compile time is not possible because
# typeNode can't be converted to typedesc.
f(typeNode)
aMacro(1)
aMacro("string")
Error:
test.nim(13, 4) Error: type mismatch: got (NimNode)
but expected one of:
proc f(T: typedesc[SomeInteger])
proc f(T: typedesc[string])
Note that there is a workaround: Doubling the argument of the macro into aMacro(x: type, typedescOfX: typedesc), and one could write a wrapper macro that takes a typed expression x and generates the AST aMacro(x, type(x)).
Currently,
macros.nimdoes not offer a way to convert aNimNode(obtained fromgetTypeon a typed expression) into the corresponding typedesc:Error:
Note that there is a workaround: Doubling the argument of the macro into
aMacro(x: type, typedescOfX: typedesc), and one could write a wrapper macro that takes a typed expressionxand generates the ASTaMacro(x, type(x)).