metaprograming: staticMap and such?

bearophile bearophileHUGS at lycos.com
Tue Jul 6 13:35:57 PDT 2010


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


More information about the Digitalmars-d mailing list