www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - metaprograming: staticMap and such?

reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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;
     }
}

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

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

---
Dmitry Olshansky
Jul 06 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Dmitry Olshansky:
 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 better if you add some spaces to improve readability, and generally a map has to work on an empty collection too. "The Little Schemer" book shows that it's often better to base recursivity on zero or empty or the thing that represents nothing (untested): template staticMap(alias F, T...) { static if (T.length) alias TypeTuple!(F!(T[0]), staticMap!(F, T[1 .. $])) staticMap; else alias TypeTuple!() staticMap; } Bye, bearophile
Jul 06 2010
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 07.07.2010 0:35, bearophile wrote:
 Dmitry Olshansky:
 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 better if you add some spaces to improve readability, and generally a map has to work on an empty collection too. "The Little Schemer" book shows that it's often better to base recursivity on zero or empty or the thing that represents nothing (untested):
Sure thing, my codestyle is lacking spaces - all that fancy template constructs are rather cryptic in C+, looks like I'm grown used to poorly readable constructs.
 template staticMap(alias F, T...) {
      static if (T.length)
          alias TypeTuple!(F!(T[0]), staticMap!(F, T[1 .. $])) staticMap;
      else
          alias TypeTuple!() staticMap;
 }

 Bye,
 bearophile
Thanks, that stuff works. An idea of working with empty typetuples never crossed my mind, but that, of course, should be covered. --- Dmitry Olshansky
Jul 06 2010
prev sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
Hi Dmitry,

On Tue, Jul 6, 2010 at 22:22, Dmitry Olshansky <dmitry.olsh 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
Jul 06 2010
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 07.07.2010 1:11, Philippe Sigaud wrote:
 Hi Dmitry,

 On Tue, Jul 6, 2010 at 22:22, Dmitry Olshansky <dmitry.olsh gmail.com
 <mailto:dmitry.olsh 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
Jul 06 2010