metaprograming: staticMap and such?

Dmitry Olshansky dmitry.olsh at gmail.com
Tue Jul 6 14:41:13 PDT 2010


On 07.07.2010 1:11, Philippe Sigaud wrote:
> Hi Dmitry,
>
> On Tue, Jul 6, 2010 at 22:22, Dmitry Olshansky <dmitry.olsh at gmail.com
> <mailto: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.
Ops, sorry! I'd beter stop coding at night, at certain point I was sure 
it's called StaticMap (following the module logic) and vaguely remember 
staticMap haven't worked either (maybe forgot to proper import - damn it).
Trying out now - it's here in typetuple and works fine.

> 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?
It well could be one of solid reasons to switch over to D completely.
>
> 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.
I recall encoding some regex-like parsing into typelists in C++, 
horrible mess but did the job )
> 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.

So am I as of 1 min ago...

> I had some fun with this (filtering, folding, takeWhile, ...), here is
> the result:
>
> http://svn.dsource.org/projects/dranges/trunk/dranges/docs/typetuple2.html
Thanks, looks very promising!
>
> 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

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list