On Wednesday, 21 August 2013 at 02:46:06 UTC, Dylan Knutson wrote:
Hello,

I'd like to open up discussion regarding allowing foreach loops which iterate over a tuple of types to exist outside of function bodies.

I would *LOVE* to have this.

however, once we go down that route, it would be very tempting to allow a whole lot of other compile-time imperative style programming. Which would be awesome.

I don't know how the compiler works with templates, but if it could be made to move linearly through them then we could potentially do this:

template allSatisfy(alias F, T...)
{
    foreach(t; T)
    {
        static if(!F!t)
        {
            static return false;
            //alternatively:
            //  enum allSatify = False;
            //  static return;
        }
    }
    static return true;
}

which could be extended to allow static break, static continue etc. This does rather make the argument for allowing "static foreach" as valid, but not required syntax.

Or... even allow enum and alias values to be manipulated at CT:

template staticMap(alias F, T...)
{
    static if(T.length == 0)
    {
        alias staticMap = TypeTuple!();
        static return;
    }
    alias map = F!(T[0]);
    foreach(t; T)
    {
        map = TypeTuple!(map, F!t);
    }
    alias staticMap = map;
    static return;
}

I'm favouring the 2-step eponymous thing in order to prevent any alias/enum ambiguities. Another alternative would be
static return alias blah;
static return enum blah;


Apologies for the bike-shedding, but if some thing like this worked properly, it would be a complete game-changer. We'd be light-years ahead of anyone else’s compile-time reflection.

Reply via email to