reflection based on my experience so far on compile-time meta-programming in D as a novice user: the problems

Steven Schveighoffer schveiguy at
Mon Sep 14 18:20:02 UTC 2020

On 9/14/20 1:40 PM, mw wrote:
> -- because of the points (1~3), the resulting D meta-programming version 
> is often convoluted, and the code may have very bigger distance from the 
> most straight-forward targeted function signature one have in mind from 
> the beginning. And this makes writing & (and more importantly) reading 
> (by a non-code-author) such code difficult.

You're looking at C macros, which D specifically does not do, mostly 
because of the crap that can come out of them (note you can use dpp if 
you really want them).

Your C macro I think translates pretty easily to a D mixin, because it's 
generating a complete function:

string DECL_TA_FUNC(string TA_FUNC, string[] FUNC_INS, string[] FUNC_OUTS)
    // this would be easier with string interpolation!
    return format(q{
bool %s(double[] inData, %-(double[] %s,%), %-(%s,%)) {
   %(assert %s.length == inData.length;%)
   int begin, num;
   int lookback = %s_Lookback( %(%s... you get the idea

enum MA_INS = [
    "int MA_optInTimePeriod",
    "int TA_MAType optInMAType"
enum MA_OUTS = [
mixin(DECL_TA_FUNC(TA_MA, MA_INS, MA_OUTS, somethingElse, whatever));

I didn't feel like translating that whole file. I think you can do it 
with probably 25% of the size of that C macro code, and IMO much more 
readable. No idea why you would prefer C preprocessor, D has far 
superior string manipulation capabilities.


More information about the Digitalmars-d mailing list