Metaprogramming in D : Some Real-world Examples
Bill Baxter
wbaxter at gmail.com
Fri Nov 13 11:59:13 PST 2009
On Fri, Nov 13, 2009 at 11:38 AM, Tomas Lindquist Olsen
<tomas.l.olsen at gmail.com> wrote:
> On Thu, Nov 12, 2009 at 8:24 PM, Bill Baxter <wbaxter at gmail.com> wrote:
>> On Thu, Nov 12, 2009 at 10:46 AM, Tomas Lindquist Olsen
>> <tomas.l.olsen at gmail.com> wrote:
>>> On Tue, Nov 10, 2009 at 1:27 AM, Bill Baxter <wbaxter at gmail.com> wrote:
>>>> On Mon, Nov 9, 2009 at 4:09 PM, Walter Bright
>>>> <newshound1 at digitalmars.com> wrote:
>>>>> Looks like Bill Baxter is giving a presentation on D Nov. 18!
>>>>>
>>>>> http://www.nwcpp.org/
>>>>
>>>> Yep, that's right, and I'd be quite grateful to you smart folks here
>>>> if you could share your meta-programming favorites with me! If
>>>> you've got a real-world example of meta-programming in D that you
>>>> think is particularly handy, then please send it my way
>>>>
>>>> I'm looking for small-but-useful things that are easy to explain, and
>>>> make something easier than it would be otherwise. Things like places
>>>> where static if can save your butt, or loop unrolling, and passing
>>>> code snippets to functions like in std.algorithm.
>>>>
>>>> Things like a compile-time raytracer or regexp parser (though quite
>>>> cool!) are not what I'm after. Too involved for a short talk.
>>>>
>>>> --bb
>>>>
>>>
>>> I think tuples are a good example of something that makes your life
>>> easier, lately I've been very fond of this little snippet:
>>>
>>>>>>>>>>>>>>>>>>>>>>>>>
>>>
>>> void delegate() Bind(Args...)(void delegate(Args) dg, Args args)
>>> {
>>> struct Closure
>>> {
>>> Args arguments;
>>> void delegate(Args) callee;
>>> void call()
>>> {
>>> callee(arguments);
>>> }
>>> }
>>>
>>> auto c = new Closure;
>>>
>>> // foreach not strictly necessary, but ldc currently chokes on
>>> just an assignment... I should fix that..
>>> foreach(i,a;args)
>>> c.arguments[i] = a;
>>> c.callee = dg;
>>>
>>> return &c.call;
>>> }
>>>
>>> class C
>>> {
>>> void foo(int,float) {}
>>> }
>>>
>>> void main()
>>> {
>>> auto c = new C;
>>> auto dg = Bind(&c.foo, 1, 2.0f);
>>> // register delegate somewhere
>>> }
>>>
>>> <<<<<<<<<<<<<<<<<<<<<<<<
>>>
>>> Not sure if this gets easier in C++0x , haven't read up on that...
>>
>> I think it does. C++0x has variadic templates. And some kind of
>> lambdas/closure thing. So probably it can do something similar.
>> Anyway, the meta- aspect of Bind seems kinda weak. Certainly a nice
>> use of variadic templates and closures, though.
>>
>> --bb
>>
>
> Just read the Wikipedia entries on Metaprogramming and
> Template_metaprogramming, I guess I wasn't really aware of the
> difference.
> Seems a lot harder to come up with small real-world snippets of the former.
The way I read it, template metaprogramming is a kind of
metaprogramming. Metaprogramming using templates.
And ctfe/string-mixin metaprogramming are another, macro
metaprogramming another etc.
I think the phrase "template metaprogramming" just took particularly
strong hold because of C++ where templates are always required for
accomplishing pretty much any kind of metaprogramming.
--bb
More information about the Digitalmars-d-announce
mailing list