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):

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:
--0016e6d26c9593bafe048abe7d80
Content-Type: text/plain; charset=ISO-8859-1

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 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 --0016e6d26c9593bafe048abe7d80 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">Hi Dmitry,<br><br>On Tue, Jul 6, 2010 at 22:22, = Dmitry Olshansky <span dir=3D"ltr">&lt;<a href=3D"mailto:dmitry.olsh gmail.= com">dmitry.olsh gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gm= ail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(2= 04, 204, 204); padding-left: 1ex;"> Hello there!<br> <br> Got my TDPL some week ago, being playing with D for a while.<br> Excited with results I&#39;m porting my personal C++ codebase now.<br> So far everything was kinda cool - even better (not to mention huge code sa= vings).<br> <br> Now I&#39;m messing with metaprograming. Simple task at hand: =A0given type= tuple of types produce typetuple of arrays of corresponding types.<br> Built in typetuples - great, but here&#39;s where some usual (boost MPL-lik= e) library stuff has gone missing.<br> <br> First, StaticMap described in phobos docs simply not exists in typetuple.d = (using dmd 2.047), here is my drop-in replacement:<br> <br> template staticMap(alias F,T...){<br> =A0 =A0static if(T.length &gt; 1){<br> =A0 =A0 =A0 =A0alias TypeTuple!(F!(T[0]),staticMap!(F,T[1..$])) staticMap;= <br> =A0 =A0}else{<br> =A0 =A0 =A0 =A0alias TypeTuple!(F!(T[0])) staticMap;<br> =A0 =A0}<br> }<br> <br></blockquote><div><br>It&#39;s in std.typetuple, but it&#39;s called st= aticMap with a small &#39;s&#39;. I agree it should be called StaticMap (bi= g S), as it becomes a type.<br><br>I think you should have a case for T.len= gth =3D=3D 0. One can map the empty typetuple, after all, particularly if t= his tuple is the result of another template and can be void in some circums= tances. <br> Ah, I see bearophile also said this. Hmm, same time zone, Leonardo :-)<br><= br>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt = 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Yes, and also having some simple meta-functors packaged along couldn&#39;t = hurt. Something like :<br> template addArray(T){ alias T[] addArray; =A0}<br></blockquote><div><br>And= isn&#39;t that cool that meta-programming (or rather, type manipulation) i= s so simple in D?<br><br>alias TypeTuple!(int, double, string) Types;<br> alias StaticMap!(addArray, Types) ArrayTypes; // ArrayTypes is TypeTuple!(i= nt[], double[], string[]) <br></div><div>=A0</div><blockquote class=3D"gmai= l_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204= , 204, 204); padding-left: 1ex;"> So the question boils down to: what are you guys using std.typetuple for me= taprograming and what&#39;s it&#39;s status?<font color=3D"#888888"><br></f= ont></blockquote><div><br>I&#39;m using it a lot, mainly for two reasons:<b= r> <br>- &#39;auto&#39;=A0 is still buggy: auto function do not show in docume= ntation comments and (worse) type deduction through many invocation of &#39= ;auto&#39; 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 deepl= y nested functions. Templates, meta-templates(!), etc. I gather I&#39;ll ge= t rid of them when the auto propagation will see its bugs squashed.<br> <br>- for fun, to construct and encode things in types :-)=A0 Peano numbers= , trees, etc.<br></div></div><br><br>As for its status, I consider it a pre= tty solid part of Phobos, if somewhat obscure. No bugs I know of (well, exc= ept the _s/S_taticMap calling convention), a good example of type manipulat= ion in D. It gave me lots of ideas.<br> <br>I had some fun with this (filtering, folding, takeWhile, ...), here is = the result:<br><br><a href=3D"http://svn.dsource.org/projects/dranges/trunk= /dranges/docs/typetuple2.html">http://svn.dsource.org/projects/dranges/trun= k/dranges/docs/typetuple2.html</a><br> <br>Uh oh, this page is looking for some documentation. All the fun new tem= plates I put there are not correctly documented. I will do that.<br><br><br=
Philippe<br>

--0016e6d26c9593bafe048abe7d80--
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.

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?

 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.

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

 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