case statement allows for runtime values, a case of accepts-invalid?
Andrej Mitrovic
none at none.none
Tue Apr 19 05:38:00 PDT 2011
int foo(ref int y)
{
y = 5;
return y;
}
void main()
{
int x = 1;
int y = 2;
switch (x = foo(y))
{
case y:
writeln("x == y");
default:
}
assert(x == 5);
assert(y == 5);
}
According to the docs:
The case expressions must all evaluate to a constant value or array, or a runtime initialized const or immutable variable of integral type.
In fact if you try to add a constant, only then will you get an error:
switch (x)
{
case y + 1:
}
Error: case must be a string or an integral constant, not y + 1
It will also error out if you try to use a field of a struct. Which leads me to believe the first case should not be allowed to compile. Thoughts?
More information about the Digitalmars-d-learn
mailing list