Users of my software will create some custom types inheriting from a Base type. I want to automatically detect the fields of all derived types, find the joined unique set, and create some special types and procs based on the names of the fields of that set. I'm not sure how best to do this.
I've tried templates, which don't have true access to global scope. I've tried macro pragmas and so far this is best. Each macro pragma adds the name to a global list. Then toward the end of my source I loop through the names and do something appropriately, like creating new structures or whatnot. Thanks.
global list. Then toward the end of my source I loop through the names
Use a HashSet instead of a list since you want a joint unique set.
Otherwise, this sounds simple and maintainable, which is huge benefit for macro. Don't try to be too clever because you'll have to maintain this forever.
Okay, thanks.
Along these lines, why is it that strings in a static block aren't static? My macros are complaining that the strings in my set I'm passing in 1 at a time aren't static, so I can't build the AST I want. I guess my method outlined above doesn't fully work.
Let's say you have a macro
macro foo(x: string): untyped =
echo x
If you use a runtime string it will say that you have a NimNode and note a string. If you use a compile-time string, once in the macro ergo within Nim compile-time VM it is considered "runtime".
This also means that this is the wrong signature
macro foo_incorrect(x: static string): untyped =
echo x
There is no static within a macro context. That would require a "pre-compile-time" VM within the compile-time VM