Will macros work with expressions?

Reiner Pope some at address.com
Mon Sep 3 01:20:59 PDT 2007


Bill Baxter wrote:
> Xinok wrote:
>> 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.
>>>
> 
>  > I agree that macro expressions should be allowed. However, I think a
>  > better syntax would be:
>  >
>  > macro foo(e) = e + 3;
>  >
> 
> I like that. But what about an expression with a few preliminary 
> statements setting up temp variables and such?
> 
> --bb

If it's going to be "pasted" into the call-site, then how can that work? 
I imagine you'd have to set it up as a function call.

Of course, a nicer solution is a syntax where (nearly) everything is an 
expression, like many functional languages.

    -- Reiner



More information about the Digitalmars-d mailing list