On Reddit there is a thread about Andrei the others discussing about C++11 and D:

http://www.reddit.com/r/programming/comments/ykxio/alexandrescu_meyers_sutter_on_static_if_c11_in/

Part of the discussion is about a "static for". We already have a static foreach in D, it's just unlabeled as "static".

I'd like "foreach" to require a "static" label before it when you use it on tuples, this makes it simpler for the programmer to tell them apart, so it increases code readability. This the step 1 I have discussed here little:
http://d.puremagic.com/issues/show_bug.cgi?id=4085


I think Andrei was also talking about doing the job of a static foreach with recursion, but today in D I use the D static foreach in situations where a recursion is not what I want, like creating cases for a normal switch:


void op(char c)() {
    if (stack.length < 2)
        throw new Exception("Wrong expression.");
    stack[$ - 2] = mixin("stack[$ - 2]" ~ c ~ "stack[$ - 1]");
    stack.popBack();
}
// ...
double[] stack;
int[] digits;
foreach (const char c; readln())
    switch (c) {
        case ' ', '\t', '\n': break;
        case '1': .. case '9':
            stack ~= c - '0';
            digits ~= c - '0';
            break;
        foreach (o; TypeTuple!('+', '-', '*', '/')) {
            case o: op!o(); break;
        }
        break;
        default: throw new Exception("Wrong char: " ~ c);
    }


In that bug report you also see three Iota() templates that implement a poor's man static foreach on integer intervals. They are handy but you can't use them outside functions.

Bye,
bearophile

Reply via email to