Is metaprogramming useful?

Georg Wrede georg.wrede at nospam.org
Thu Nov 30 19:32:25 PST 2006


Jarrett Billingsley wrote:
> "Georg Wrede" <georg.wrede at nospam.org> wrote in message 
> news:456EBAFF.10000 at nospam.org...
> 
>>Not necessarily.
>>
>>If we wanted the metalanguage to let us do a hairy definition that, say, 
>>lets us use ][ as an operator, then yes, it breaks the separation.
>>
>>But if we restrict ourselves to the kind of things done in the "unless" 
>>example, then there is no risk of this breakage.
> 
> How so?  The 'define' statement would be a statement like anything else, 
> which would not be recognized until the syntactic pass.  Since the 'unless' 
> construct makes a construct that doesn't exist in the language, how would D 
> know how to parse 'unless' statements unless it parses and semantics the 
> 'define' statement first?
> 
> void foo(bool cond)
> {
>     unless(cond)
>     {
>         writefln("not cond");
>     }
> }
> 
> define("unless", "(", BooleanExpression, ")", BlockStatement)
> {
>     if(!BooleanExpression) BlockStatement;
> }
> 
> When D does the semantic pass on this, it semantics 'foo' first, and so 
> would fail to parse the 'unless' statement correctly.  Although, now that I 
> think of it, D's semantic pass is split up, and the first pass only 
> semantics the function signature and not the body, so in this case, the 
> 'define' would be semantic'ed before foo's body, and it'd work.  So..  :)

Whew!

> Well there!  I made your job easier :)  Although I wonder if there'd be odd 
> corner cases regarding things like templates.

Today you can define a function and instantiate it within a template 
body, and you can instantiate that template within another function 
body. So far, I see no fundamental reason why meta-definitions should be 
any different.

>>You can think of it like this: had Walter wanted to implement "unless" 
>>last week in D, we'd have no problem today with the separation, right? So, 
>>if our metalanguage lets us do only things "Walter could have done without 
>>touching the parser", then this separation issue doesn't exist.
>>
>>
>>>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.
>>
>>It doesn't have to be scary. All it means is that some things (like 
>>"unless" above) are valid somewhere in the code, and not valid somewhere 
>>else. The word for that is scope. We do it currently with functions, and 
>>nobody complains.
>>
>>So you could look at the meta definition like just an ordinary function 
>>definition. I honestly think that if we'd had this in D all along, folks 
>>would hardly notice. 
> 
> 
> 



More information about the Digitalmars-d mailing list