Is metaprogramming useful?

Don Clugston dac at nospam.com.au
Thu Nov 30 01:01:25 PST 2006


Georg Wrede wrote:
> renoX wrote:
>> Back on the subject of metaprogamming, one thing which makes me 
>> cautious about
>> metaprogramming is debugging: when there is a problem debugging 
>> generated code is
>> a nightmare usually..
> 
> It's not metaprogramming itself, it's the bad implementations that make 
> it hard.
> 
> If we had Perfect Metaprogramming(TM) in D, then I could do the following:
> 
>   I'm coding some stuff and I notice that what I'd really want
>   is a new keyword, "unless", that would make it so much easier
>   for me to write this application clearly. I decide to create it.
> 
>   I want to use it like this
> 
>     unless (fullMoonTonight) { doRegularStuff() }
> 
>   So, to create such a thing in D, I'd write something like
> 
>     define("unless", "(", BooleanExpression, ")", BlockStatement)
>     {
>       if(!BooleanExpression) BlockSTatement;
>     }

What's wrong with

void unless(bool cond, lazy void delegate() blockstatement) {
    if (!cond) blockstatement();
}

?

Sure, you have to write

unless(fullMoonTonight, { doRegularStuff(); });

but if there was a way to have trailing delegates, you could have the 
exact syntax you wanted.

(The interaction between lazy parameters and template metaprogramming is 
a really interesting unexplored area. What can be achieved with tuples 
containing lazy code blocks???).

> ---
> 
> People say GC has to be slow, but that is mostly because it used to only 
> exist in languages that were slow to begin with (and badly implemented).
> 
> The same thing with metaprogramming. C++ has given it such a bad rep, 
> it'll take years before folks learn away from their fears.

Absolutely.



More information about the Digitalmars-d mailing list