Will macros work with expressions?

Don Clugston dac at nospam.com.au
Mon Sep 3 02:35:57 PDT 2007


Bill Baxter wrote:
> A question about the upcoming macros:
> Will they work for expressions as well as statements?
> 
> The examples in WalterAndrei.pdf all show complete statements,
> like
>    macro foo(e) { e=3; }
> 
> But will it be possible to have:
>    macro foo(e) { e+3 }
> 
> I hope expressions will be allowed.  Not for things like e+3 but for 
> making local shortcuts where aliases don't cut it:
> 
>    macro call_func( arg ) {
>       $this.some_member.a_really_long_named_template_member(arg);
>    }
>    ...
>    writefln(call_func(1));
>    writefln(call_func("hi"));
> 
> And I'm sure there'd be any number of other uses for expression macros.

If I understand it correctly, that should work, since macros operate via syntax 
tree substitution.

Unfortunately, it seems that macros will be far less powerful than text mixins. 
I was hoping that I'd be able to replace most of my uses of text mixins with 
macros, but found very few places where it is possible :(.
Sadly, I think I'll only be using macro + .stringof for a syntax sugar wrapper 
around a text mixin. Assuming .stringof will work, which is not guaranteed.

Hopefully this will work:

macro ASSERT(e) {
   assert(e, e.stringof ~ " failed");
}



More information about the Digitalmars-d mailing list