In a not-ranged cases body, like in the program below (that doesn't compile), 
the switch variable is a compile-time constant, so why doesn't the compile see 
x as constant there?


template Foo(uint x) {
    static if (x <= 1)
        enum Foo = 1;
    else
        enum Foo = x * Foo!(x - 1);
}

int bar(uint x) {
    switch (x) {
        case 0: return Foo!x;
        case 1: return Foo!x;
        case 2: return Foo!x;
        case 3: return Foo!x;
        case 4: return Foo!x;
        default: return -1;
    }
}

void main() {
    assert(bar(4) == 24);
}


That code works if I replace lines like:
case 2: return Foo!x;

With:
case 2: return Foo!2;

But when the code isn't DRY bugs may happen...
(There are ten different better ways to write that program, but this is not the 
point).

Bye,
bearophile

Reply via email to