Is it possible to use tokens as template parameters?

rempas rempas at tutanota.com
Sat Aug 14 13:50:52 UTC 2021


On Saturday, 14 August 2021 at 12:19:59 UTC, Tejas wrote:
> On Saturday, 14 August 2021 at 11:23:12 UTC, rempas wrote:
>> Is it possible to do something like that?
>>
>> ```
>> mixin template lel(alias N, alias V) {
>>   auto N = V;
>> }
>>
>> void main() {
>>   mixin lel!(name, "Hmmmm");
>> }
>> ```
>>
>> In this case, it would (if it was possible) get replaced with: 
>> `auto name = "Hmmmm";`
>> Is there something I'm missing?
>
> You have to use mixins to achieve the desired effect.
>
> ```d
> mixin template lel(T, string N, string V) {
>     mixin(T.stringof ~ " " ~N ~ " = "  ~ V ~ ";");
> }
>
> import std;
> void main(){
>     mixin lel!(string, "name", ` "hmmmm" `);
>     writeln(name);
> }
> }
> ```

The behavior I want to achieve is not having to enclose the word 
(tokens) in double quotes.


More information about the Digitalmars-d mailing list