AST macros

eao197 eao197 at intervale.ru
Sun Mar 18 13:57:07 PDT 2007


On Sun, 18 Mar 2007 23:30:54 +0300, Walter Bright  
<newshound at digitalmars.com> wrote:

> eao197 wrote:
>> On Sun, 18 Mar 2007 05:00:34 +0300, Walter Bright  
>> <newshound at digitalmars.com> wrote:
>>
>>> Right now, I see AST macros as manipulating expressions and  
>>> statements, not declarations.
>>  If I have understood you correctly the D macros will be used like  
>> class_eval/module_eval in Ruby -- for generation new content of the  
>> programm. For example, in Ruby I can write:
>
> Macros wouldn't be for adding declarations to classes, for that use  
> template mixins.

I don't know how use template mixins for such case:

// It isn't real D.
template ValueAccessor(Type,Name) {
   Type Name;
   bool is_<Name>_defined;
   Type get<Name>() {
     if( !is_<Name>_defined ) throw new ValueNotDefined("<Name>");
     return Name;
   }
   void set<Name>( Type value ) {
     Name = value;
     is_<Name>_defined = true;
   }
}

class SomeValueHolder {
   mixin ValueAccessor!( uint, DataCoding );
   mixin ValueAccessor!( char[], SourceAddress );
   mixin ValueAccessor!( char[], DestinationAddress );
   ... // And another bunch of ValueAccessor calls.
}

auto holder = new SomeValueHolder;
holder.setDataCoding( 0x01 );
holder.setSourceAddress( "..." );

This can be done via compile-time functions and mixin expression, but I  
don't think it is possible via template mixins.

So I think it is better for me to wait while you implement your macro  
system in D and see the result. I hope it will be very interesting. Good  
luck!

Thank you for your patience.

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list