I hear that the D language has powerful metaprogramming features for executing functions at compile time. That sounds very exciting, but I find it difficult to think of prac
If you want practical examples of how to use D's metaprogramming facilities (CTFE, or compile time function evaluation, is just one of these, and not even the most important one) look no further than Phobos, the D2 standard library. Much of this code is written by Andrei Alexandrescu, who invented a lot of the template metaprogramming techniques in C++, and is now working with Walter Bright on the design and implementation of D.
The best modules to look in are std.range and std.algorithm. These are almost entirely composed of templates, were designed by Andrei, and are surprisingly readable given the amount of metaprogramming they use. I've contributed significantly to both of these modules and reading the code that was there when I started was basically how I learned.
All of the code is licensed under the (extremely permissive) Boost license and can be viewed directly from your browser at the Phobos Trac site on dsource.org.
To give you a roadmap of what you're looking at, D's metaprogramming facilities basically fall into 4 categories:
Templates, which are basically like C++ templates, but with some added features like static if,static assert, variadic templates, and constraints, which are basically like concepts but simpler.
Compile time reflection/introspection. This includes the builtin is() expressions and __traits, as well as the standard library module std.traits.
Mixins. These allow you to take either a template (template mixins) or a compile time string (string mixins) and evaluate it as code in the current scope. String mixins can be thought of as being kind of like an eval statement, except that the string is evaluated as code at compile time instead of at runtime.
Compile time function evaluation, or CTFE, which allows functions that meet certain criteria to be evaluated at compile time. One important use of CTFE is that, combined with string mixins, you can generate code as a string at compile time, and then evaluate it as code in the scope where the mixin statement occurs. For examples of this, see std.range.Lockstep and std.range.OutputRangeObject, which I recently checked into the SVN releases of Phobos.