Will macros work with expressions?

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Tue Sep 4 09:00:30 PDT 2007


Bill Baxter wrote:

> Robert Fraser wrote:

>> Walter discussed this during his talk I believe. Indeed, macros will
>> do syntax-tree substitution and variable declarations are becoming
>> expressions, so you'll be able to insert temporary variables, etc.
> 
> Variable declarations will become expressions!?  That's interesting.  I
> wonder what the scope will be for such things.
> 
>> My worry is that macros used inside expressions will not be able to
>> contain statements. This is a shame, since this means that macros and
>> function calls won't be interchangeable. Looks like macros will be
>> slightly more powerful than their C counterparts, which I've never
>> used.
> 
> I dunno about their "power", but At least they'll be significantly safer
> and more robust than their C counterparts.

I would say they have less expressiveness than C counterparts unless all
statements suddenly become expressions. However, there is no real need to
do this. Another way could be to introduce a new compile time
metaexpression/statement type. Maybe the "D code string" does exactly this?
After this another problem pops up: the reflection system isn't really
designed for this low level operations. I'm not saying this is wrong
direction though, quite opposite. It just shows how broken the C-based
language core is for metaprogramming. 

One more problem I see in the new macro feature is that it's basically (or
is it - the proposal is a bit short on details) a generic cut'n'paste
function. Is it possible to control (i.e. is it a Turing complete compile
time function call) the output of a macro ? Pseudo code example:

macro(foo) {
  metaif (foo == bar) {
    metareturn runtimecode{ while(foo.next !is null) { print(foo.next); foo
= foo.next; }
  } metaelse {
    runtimecode ret;

    metaforeach(a; foo) {
      ret ~= runtimecode{ a.bar *= 2; }
    }

    metareturn ret;
  }
}

( this is basically CTFE+mixins, but on the AST level, not with strings. I
could do this even today by implementing a D compiler with AST macros on
top of D with metaprogramming :P )

If I understood it correctly, the macros only do a simple substitution using
their parameters, hide symbols when necessary (hygiene) and then attach the
generated code to the AST.



More information about the Digitalmars-d mailing list