www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - On Static If and more

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


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:

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]");
// ...
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';
         foreach (o; TypeTuple!('+', '-', '*', '/')) {
             case o: op!o(); 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.

Aug 21 2012