reflection based on my experience so far on compile-time meta-programming in D as a novice user: the problems
FeepingCreature
feepingcreature at gmail.com
Mon Sep 14 08:01:47 UTC 2020
On Sunday, 13 September 2020 at 18:32:17 UTC, mw wrote:
> -- in D, there are too many choices, with no clear guideline
> which one is *THE* one to use for a particular purpose:
> language or library mechanisms? mixin? template? AliasSeq /
> aliasSeqOf? Tuple? (non-)?-eponymous version of ...?; and even
> for a particular option, e.g. Tuple!(int, "MA_optInTimePeriod",
> TA_MAType, "opInMAType"), there are choices to use either
> token (int) or string ("MA_optInTimePeriod"). And user does not
> have a strong guide on which choice is *THE* way to proceed.
> Each mechanism seems to have / fit a particular purpose, but
> when you start to use it, you'll probably find there are new
> problems come-in later, and you want to revisit the choice you
> made earlier on.
>
> By contrast, in C: there is only *ONE* mechanism, i.e. macro,
> that's it.
>
In Assembly, there is no choice between while and do/while and
for; there is only jmp. High-level structured expression is still
preferable.
> -- in D, there is no easy way to convert between token <==>
> string. Given a token, does token.stringof always work to paste
> with other strings to generate a new token? and given a string,
> does mixin!"string" always work to be a valid token?
>
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!
> -- in D, there is no easy way to see directly the generated
> source code by the compiler at compile-time, which makes the
> debug difficult during development.
`pragma(msg, X.stringof)`. This is what stringof is for. I could
not imagine D metaprogramming without pragma(msg).
More information about the Digitalmars-d
mailing list