How to insert code in place with templates/mixins?

rempas rempas at tutanota.com
Mon Dec 20 18:03:09 UTC 2021


On Monday, 20 December 2021 at 11:30:09 UTC, rumbu wrote:
>
> Enums (that's why the string is declarated as enum) are 
> evaluated at compile time, the concatenation op will not end in 
> your code as instruction, so you can do anything outside 
> betterC rules as long you do it at compile time. You are just 
> building some code to use later, the compiler does not generate 
> any instruction for it.
>
> In the example above you can press the AST button to see 
> exactly how your code is generated.
>
> Wnen you have doubts about a generated string you can always 
> test it with ```pragma msg```. In this case, if you write:
>
> ```
> pragma(msg, add_char!'%');
> ```
>
> you will have in the output exactly what the compiler will 
> generate for your mixin.

That's cool! And I was wondering how I can make sting literal 
concatenation at compile time. Now the problem is that I want it 
to get the name of so symbol and add it to a string literal. 
Let's check this example: enum state(alias name) = `name` ~ ` = 
10;`;

I want this to add the token of that will be used as name in the 
string. For example, I want `state!val;` to get "expanded" as 
`val = 10;` rather than `10 = 10;`. So I don't want it to take 
the value of "val" but the word/token "val" itself. I tried using 
`alias` instead of `char` for the parameter but it didn't worked. 
Do you know how I can do that?


More information about the Digitalmars-d-learn mailing list