how to do this meta-programming? print the address of random element's address of a variable length of arrays?

Paul Backus snarwin at
Sun Sep 13 19:10:47 UTC 2020

On Sunday, 13 September 2020 at 18:24:01 UTC, mw wrote:
> But, I'd reflect on my experience so far on compile-time 
> meta-programming in D as a novice user, the big problems are:
> -- 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.

I think the biggest issue here is that, like you said, it's hard 
to find good resources for learning how to use D's various 
reflection and code-generation facilities. In the community 
Discord, we've tried to collect some links in the #resources 
channel, which you might find helpful:

- Philippe Sigaud's "D Template Tutorial"

- Bradley Chatha's "Intro to D metaprogramming"

- Vladimir Panteleev's "Functional Image Processing in D"

Ideally, we'd have some official, easy-to-find place where 
tutorials like this could be collected (maybe on the Wiki?), but 
so far no one's taken the initiative to set that up. One of the 
downsides of having a volunteer community, I suppose.

> -- 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's no 100% reliable way to round-trip a token to a string 
and back again, because the same token can have a different 
meaning depending on what scope it's used in. Adam Ruppe goes 
over some alternatives to string mixins you can use to avoid this 
pitfall in one of his "Tips of the Week":

> -- 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.
> By contrast, in C: one can easily see the result via: cpp -P 
> foo.h > foo.c

I agree that this is an issue. There are a couple compiler flags 
that can help (-mixin=filename, -vcg-ast), but mostly you're 
stuck with printf-style debugging via `pragma(msg)`.

More information about the Digitalmars-d-learn mailing list