In nim, if I am writing a macro, how do I explicitly construct a name from an argument. Specifically in the nim manual (see here) we have the following example of "identifier construction"
template typedef(name: untyped, typ: typedesc) =
type
`T name`* {.inject.} = typ
`P name`* {.inject.} = ref `T name`
typedef(myint, int)
var x: PMyInt
How can I accomplish the same thing if I want typedef to be a macro instead of a template?
any template can trivially become a macro. here:
but that's not helpful.
if you find yourself needing to step up from template to macro-land, it's because you need to manipulate the ast directly. so the first thing you do is figure out what the ast of your desired output is:
dumpAstGenwill output the exact code you'd need to put into your macro but it's less clear visually, so i'm usingdumpTree.now we make a macro that constructs that exact ast:
at the end we echo the ast we just made so we can compare it against what we were expecting
here it is as
echo result.reprjust for clarity: