3 ms·
> Something I don't see here is macros that generate other macros, but the question is how much you really want that anyway You can do that in Nim in a readabl
by arc776 7y ago
> Something I don't see here is macros that generate other macros, but the question is how much you really want that anyway
You can do that in Nim in a readable way.
import macros
macro genMacro(name: untyped): untyped =
result = quote do:
macro `name`: untyped =
result = quote do:
echo "Foo"
genMacro(bar)
bar # Generate and perform the echo
Surprisingly I've actually used this kind of thing!
In one of my projects I use a macro to parse a set of types for fields and generate constructor macros for them.
The generated constructor macro passes through the parameters it's given to the default built-in constructor but does some setup before.
The final generated code is a normal built-in construction without proc calling yet with special fields initialised automatically.
- thsealienbstrds 7y ago> In one of my projects I use a macro to parse a set of types for fields and generate constructor macros for them. Funny, I did a similar thing! :)