Simple template and macro question

2023-09-27 Thread dlesnoff
> Would you mind explaining the distinction between the generic parameters of > something like a template/macro vs the actual template/macro arguments? I am not a Nim expert, I program in Nim just as an hobby. The following sections in the Nim manual can be of help to you: *

Simple template and macro question

2023-09-26 Thread CircArgs
Kinda close, but the description says > will actually dump x + y, but at the same time will print at compile time the > expansion of... its definition is just echo body.toStrLit result = body Run and it takes `typed` and I can't avoid compiler errors while using it. W

Simple template and macro question

2023-09-26 Thread Zoom
The explanations in this thread are great and the only logical next step is to add it all to the official tutorials.

Simple template and macro question

2023-09-26 Thread nrk
> Is there a way to actually see the expanded Nim code. Maybe try [expandMacros](https://nim-lang.org/docs/macros.html#expandMacros.m%2Ctyped).

Simple template and macro question

2023-09-26 Thread CircArgs
For `repr` though, it seems is has to be in a macro (I'd been using it before asking this question actually and I didn't even put together that the `gensym` I was seeing was actually modifying the identifiers!) 🤔 I suppose I can just make a utility macro for the repr in which case I guess my qu

Simple template and macro question

2023-09-26 Thread dlesnoff
There is nothing in your code saying that T should be char and R should be a string. Parser seems to be a generic type that is not specialized. Shouldn't that be: 1\. Just to see if it works correctly: template generate(body: untyped): untyped = block: proc temp[T, R]

Simple template and macro question

2023-09-26 Thread dlesnoff
> Is there a way to actually see the expanded Nim code. Like your comment Do like the code snippet above. Return the `repr` of the Nim's AST.

Simple template and macro question

2023-09-25 Thread CircArgs
Thanks all @PMunch , choltreppe, and @nrk! @nrk I'd seen those docs, but I couldn't see why even the proc parameters would be gensym'd. Is there a way to actually see the expanded Nim code. Like your comment: # works, as it expands to something like: # let x`gensym0 = "hello wo

Simple template and macro question

2023-09-25 Thread choltreppe
> In general, variables & types are gensym'ed, procedures and the like are > inject'ed. This is why you can call add without an inject pragma. This is not the reason. The symbol is inject because `name` is a parameter of the template. But it would also be injected if it wasn't because its a proc

Simple template and macro question

2023-09-25 Thread PMunch
What @nrk says is completely correct. But just for completeness sake I have some additional comments. By default symbols are gensym'ed, meaning they get non-colliding names. But we can explicitly name the arguments like so: macro createProc(name: untyped, body: untyped): untyped =

Simple template and macro question

2023-09-24 Thread nrk
You have encountered symbol injection & gensym. By default, symbols of variable names in templates (and `quote`) are gensym'ed. This means that they receive a unique identifier only used inside the context of said template. You can in fact inspect this behavior using `repr`: impor

Simple template and macro question

2023-09-24 Thread CircArgs
I'm tinkering with templates and macros to learn. I've had success with full ast-based macros but am trying to use some of the sugar for macros and some templates. I've tried some variations of the below simple example without success. The compiler complains about undeclared `x, y`.