Notes on D future

Reiner Pope some at address.com
Wed Aug 29 01:51:25 PDT 2007


Reiner Pope wrote:
> PS. The other related thing I would really like to see is the ability to 
> manipulate types with standard (ie non-template) syntax. The problem at 
> the moment is that you can't use CTFE when doing operations on types. 
> What I would like is some type, for instance Type, which you could use 
> in normal parameter lists. It would always have to be a static 
> parameter, but it could be manipulated with arrays, etc. Then,
> 
> template Map(alias Fn, T...)
> {
>     static if (T.length == 0)
>        alias Tuple!() Map;
>     else
>        alias Tuple!(Fn!(T[0]).val, Map!(Fn, T[1..$])) Map;
> }
> 
> would become
> 
> Type[] map(alias Fn)(static Type[] types)
> {
>     Type[] res;
>     foreach (t; types)
>         res ~= Fn!(t);
>     return res;
> }
> 
> I know the details are missing, but I live in hope. :-)

I think I may have accidentally explained the use of static foreach 
here. <g>

This seems like a perfect candidate for static foreach, as well as 
explaining where static foreach is useful: where the loop *absolutely* 
*must* be unrolled since the semantics may be different for the 
different bodies (given that the types are different, semantic analysis 
must be done multiple times).

With static foreach, it might be

template Map(alias Fn, Types...)
{
     alias Tuple!() Map;
     static foreach (T; Types)
         alias Tuple!(Map, Fn!(T)) Map;
}

although this relies on the dubious feature of redefining Map...


    -- Reiner



More information about the Digitalmars-d mailing list