reflection based on my experience so far on compile-time meta-programming in D as a novice user: the problems
Paul Backus
snarwin at gmail.com
Mon Sep 14 10:54:07 UTC 2020
On Monday, 14 September 2020 at 09:14:42 UTC, Sebastiaan Koppe
wrote:
> On Monday, 14 September 2020 at 08:01:47 UTC, FeepingCreature
> wrote:
>> There is an easy way to convert between a token and a string:
>> "token". Seriously, stringof should be renamed to
>> stringof__debugOnlyReallyYouDontNeedThisForMetaprogrammingTrustMe. It's like catnip to C people. I don't know if the docs have a big fat warning saying "DO NOT USE THIS FOR METAPROGRAMMING EXCEPT FOR DEBUGGING AND ERROR MESSAGES, TRUST ME" but they probably should. Just use the name of the token metavariable!
>
> I have been trying, but there is one I can't get...
>
> ---
>
> import std;
>
> string generate(T)() {
> return "%s bla;".format(T.stringof);
> }
>
> void main() {
> mixin(generate!int);
> bla = 5;
> }
Don't overthink it:
string generate(string typeName)() {
return "%s bla;".format(typeName);
}
void main() {
mixin(generate!"int");
bla = 5;
}
More information about the Digitalmars-d
mailing list