AST macros

Daniel Keep daniel.keep.lists at gmail.com
Mon Mar 19 20:53:03 PDT 2007


Walter,

I've just been going back over my old (and now fairly crusty) coroutine
implementation, and realised that macros could make these a *major*
feature of D.

Ideally, I'd like to see a syntax like this:

> coro showfiles(char[] msg)(char[], char[]) // <-- input,output types
> {
>     char[] path;
>     while( (path = yield) != null ) // <-- yield is a protected method
>     {
>         writefln("%s: %s", msg, path);
>     }
> }

Which a macro could then unwind into something ugly that the user
doesn't have to see:

> class showfiles : CoroutineT!(char[], char[], char[])
> {
>     mixin CoroutineMixin!(char[], char[], char[]);
>     void run(char[] msg)
>     {
>         char[] path;
>         while( (path = this.yield) != null )
>         {
>             writefln("%s: %s", msg, path);
>         }
>     }
> }

Which is basically how it looks now.  Then, using it is nice and clean:

> scope sf = showfiles("Hi there");
> sf("file1");
> sf("file2");

I've tried to come up with other syntaxes using current constructs, and
I can't really get any better than having the user define a subclass,
mixin a template, and then implement a run method.  It just feels...
unwieldy.

Will the sort of syntax in the first example be possible?  If not, do
you have any thoughts on how to improve the above kind of construct
(either now or with D 2.0)?

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list