What is the coolest somewhat practical metaprogramming hack you've done or seen done in the D programming language? Somewhat practical means excluding, for example, the compile-time raytracer.
The D/Objective-C Bridge uses templates to let you manipulate Cocoa objects in D.
DParse in Scrapple tools is a templated parser generator. However, ldc is the only D compiler with a functioning compile time GC (but even then it has a couple oddly random crashes). Ive played with it a little and you can do some interesting things like config file parsing and stuff, but until a compile time GC is fully running you cannot do big things.
My favorites would be ElemType and KeyType from tools.base:
template ElemType(T) {
alias typeof((function() {
foreach (elem; Init!(T)) return elem; assert(false);
})()) ElemType;
}
template KeyType(T) {
alias typeof((function() {
foreach (key, elem; Init!(T)) return key; assert(false);
})()) KeyType;
}
Compile time string hashing. You can use this to obfuscate embedded strings in your code. Just search for "hash". quite a few other interesting samples on that page, too.
A united type template struct (It wont allow you to make unit errors.)
An arbitrary precision type It generates ASM code at compile time (before the compiler does)
In terms of the outright coolest, I'd have to say Kirk McDonald's PyD (and other similar bindings) as these have do to a huge amount of work in detecting and handling lots of different types, as well as complex code generation.
That said, PyD only wins because BLADE technically uses CTFE, not templates.
On a more personal note, D templates have gotten extensive use in a research project of mine. It's a simulation framework where modules can define their own private data types. Exposing a new user type to the framework requires a single line of code which creates an XML parser for the type as well as associated network serialisation/deserialisation code.
I'll answer my own question because this one didn't exist when I asked it. I wrote a patch to the garbage collector that uses templates and compile time introspection to generate the pointer offset information for arbitrarily complex user defined types to allow for precise heap scanning, instead of having this be done within the compiler.