Switch constants
BCS
anon at anon.com
Sun Nov 14 09:08:38 PST 2010
Hello bearophile,
> 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;
> }
> }
If you want exactly that:
switch(x) {
foreach(X; Tuple!(0,1,2,3,4)) {
case X: return Foo!X;
}
}
More information about the Digitalmars-d-learn
mailing list