AST macros

Walter Bright newshound at digitalmars.com
Sat Mar 17 18:55:54 PDT 2007


Marcin Kuszczak wrote:
 > Walter Bright wrote:
 >> It's pretty simple:
 >>
 >> macro foo(args)
 >> {
 >> ...syntax that gets inserted...
 >> }
 >>
 >> and the args and syntax is evaluated in the context of the 
invocation  of
 >> the macro, not the definition of the macro. You'll be able to do things
 >> like:
 >>
 >> macro print(arg)
 >> {
 >> writefln(__FILE__, __LINE__, arg);
 >> }
 >>
 >> which get the file and line in the right context. There's no way to do
 >> that right now.
 >
 > And what about mixin templates? Couldn't they just be fixed to do 
this work?
 >
 > From docs:
 > " Unlike a template instantiation, a template mixin's body is evaluated
 > within the scope where the mixin appears, not where the template
 > declaration is defined. It is analogous to cutting and pasting the 
body of
 > the template into the location of the mixin. It is useful for injecting
 > parameterized 'boilerplate' code, as well as for creating templated 
nested
 > functions, which is not possible with template instantiations. "
 >
 > For me it looks exactly like this what you want to do with macro.
 >
 > What would be a difference?

Mixin templates have a fundamental problem with this - the mixin 
arguments are semantically evaluated before the mixin is instantiated. 
To manipulate AST's, it's necessary to get at them before the semantic 
analysis is done.

 > For me it seems that the whole mixin thing is rather buggy and 
unusfull at
 > the moment - for reference see:
 > http://www.digitalmars.com/d/archives/digitalmars/D/learn/3412.html#N3416

They're not buggy, but it's possible they aren't the right design.




More information about the Digitalmars-d mailing list