Is metaprogramming useful?

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Nov 29 18:34:42 PST 2006


"Georg Wrede" <georg.wrede at nospam.org> wrote in message 
news:456E1768.7090504 at nospam.org...

> 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;
>     }
>
> Now, if I'd made errors in writing the meta code, then the compiler would 
> error me, of course. No biggie. And since the D compiler would understand 
> what's going on (as opposed to the C preprosessor or compiler), the error 
> messages would be what we're used to in D.
>
> Later, when I actually use the "unless" construct, again the error 
> messages would be normal because D now understands what "unless" is all 
> about.

Unfortunately, that then breaks the separation of the 
lexical/syntactic/semantic passes.  The define statements have to be 
syntaxed and semantic'ed before any other code can be even syntaxed.

One way around this would be to have some kind of "d metamodule" file which 
would define all the metacode.  Those metamodules would be compiled first, 
allowing the compiler to compile the normal D code.  Scary. 





More information about the Digitalmars-d mailing list