metaprograming: staticMap and such?

Philippe Sigaud philippe.sigaud at gmail.com
Tue Jul 6 14:11:01 PDT 2010


Hi Dmitry,

On Tue, Jul 6, 2010 at 22:22, Dmitry Olshansky <dmitry.olsh at gmail.com>wrote:

> Hello there!
>
> Got my TDPL some week ago, being playing with D for a while.
> Excited with results I'm porting my personal C++ codebase now.
> So far everything was kinda cool - even better (not to mention huge code
> savings).
>
> Now I'm messing with metaprograming. Simple task at hand:  given typetuple
> of types produce typetuple of arrays of corresponding types.
> Built in typetuples - great, but here's where some usual (boost MPL-like)
> library stuff has gone missing.
>
> First, StaticMap described in phobos docs simply not exists in typetuple.d
> (using dmd 2.047), here is my drop-in replacement:
>
> template staticMap(alias F,T...){
>    static if(T.length > 1){
>        alias TypeTuple!(F!(T[0]),staticMap!(F,T[1..$])) staticMap;
>    }else{
>        alias TypeTuple!(F!(T[0])) staticMap;
>    }
> }
>
>
It's in std.typetuple, but it's called staticMap with a small 's'. I agree
it should be called StaticMap (big S), as it becomes a type.

I think you should have a case for T.length == 0. One can map the empty
typetuple, after all, particularly if this tuple is the result of another
template and can be void in some circumstances.
Ah, I see bearophile also said this. Hmm, same time zone, Leonardo :-)



> Yes, and also having some simple meta-functors packaged along couldn't
> hurt. Something like :
> template addArray(T){ alias T[] addArray;  }
>

And isn't that cool that meta-programming (or rather, type manipulation) is
so simple in D?

alias TypeTuple!(int, double, string) Types;
alias StaticMap!(addArray, Types) ArrayTypes; // ArrayTypes is
TypeTuple!(int[], double[], string[])


> So the question boils down to: what are you guys using std.typetuple for
> metaprograming and what's it's status?
>

I'm using it a lot, mainly for two reasons:

- 'auto'  is still buggy: auto function do not show in documentation
comments and (worse) type deduction through many invocation of 'auto' do not
work, due to forward reference limitation. So I tried to get rid of auto as
much as I could in my code base. It made me code a whole machinery for type
manipulation, to get the correct return types for deeply nested functions.
Templates, meta-templates(!), etc. I gather I'll get rid of them when the
auto propagation will see its bugs squashed.

- for fun, to construct and encode things in types :-)  Peano numbers,
trees, etc.


As for its status, I consider it a pretty solid part of Phobos, if somewhat
obscure. No bugs I know of (well, except the _s/S_taticMap calling
convention), a good example of type manipulation in D. It gave me lots of
ideas.

I had some fun with this (filtering, folding, takeWhile, ...), here is the
result:

http://svn.dsource.org/projects/dranges/trunk/dranges/docs/typetuple2.html

Uh oh, this page is looking for some documentation. All the fun new
templates I put there are not correctly documented. I will do that.


Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100706/4b285507/attachment.html>


More information about the Digitalmars-d mailing list