Meta-programming - examples

janderson askme at me.com
Sat Feb 10 22:39:01 PST 2007


Bill Baxter wrote:
> 
> Hmm.  Me hopes more and more that something can be done about string 
> literals.  I don't really want to end up having big chunks of my code 
> stashed away in quotes.  Editors don't know how to highlight it or 
> format it, even if it's plain D on the inside.

You can always use import.


> 
> I had forgotten about this, but there's also the example of Qt's dynamic 
> signals and slots that was discussed a while back.  Qt's moc compiler 
> /sort of/ knows how to process most /garden-variety/ C++ in order to 
> extract method signatures, so that it can generate string-based run-time 
> stubs.  The stubs let you write code like:
>     connect(object1,"notifyFoo(int)", object2,"fooChanged(int)");
> To connect up object1's notifyFoo signal with object2's fooChanged 
> method.  In Qt's implementation, the moc compiler generates a 
> string->method_pointer lookup table in each QObject and some other 
> boilerplate so that the lookup can be done dynamically at runtime.  That 
> allows you to load a UI script at runtime and connect up the signals and 
> slots dynamically based on just the names of methods.
> 
> Actually, that seems like it should be mostly doable already via tuples 
> and mixins.  But instead of syntax like
>     slot void myMethod(int x) { . . . }
> you'd need
>     void myMethod(int x) { . . . }
>     mixin DynamicSlot!("myMethod");
> 
> But you'd probably need to declare all the dynamic slots in one place, like
>     mixin DynamicSlots!("myMethod",
>             "myOtherMethod",
>             "aThirdMethod");
> It would be nice if you could do them a method at a time, though, to 
> keep them close to the place where they're declared.  Even better if it 
> could be done without repeating the name.   Maybe something like
> 
>     mixin(DynamicSlot!("void myMethod(int x)")) { . . . }
> 
> Meh.  Still ugly.  It would be a lot nicer if it could just be
>     DynamicSlot! void myMethod(int x) { . . . }

Your right, I was wondering if the mixin format itself could some how be 
improved.  Although its a good way of indicating, user-specified code 
goes here.  Of course you could do something like:

mixin(DynamicSlot!("

void myMothod(int x)
{
  ...
}

"));

or defined you own language extensions:

mixin(MyLanguageExtentions!(import(foo.d));

//foo.d
DynamicSlot! void myMothod(int x)
{
  ...
}

ect...

However I'm guessing the overhead could become insane, particularly when 
people start writing:

mixin(BobsLanguageExtentions!(MyLanguageExtentions!(import(foo.d))));

Maybe if you could somehow have an efficient tokenisor and mix bobs and 
your language extensions together in one template it may be adequate. 
The time you save with the language extensions may offset the time used 
to compile the code.

> 
> Still not sure how that would manage to collect all the slot info into 
> one big function that does the run-time lookup, though.


> 
> --bb



More information about the Digitalmars-d mailing list