Ellery Newcomer wrote:
On 12/05/2009 11:17 PM, Don wrote:
Ellery Newcomer wrote:
On 12/05/2009 09:20 PM, Don wrote:
Nick Sabalausky wrote:
I just noticed in D1 that the values for the cases in a switch must be
known at compile-time (btw, the docs don't seem somewhat vague on
that). Is this also true in D2? If so, I don't suppose we could get
that changed before the book? It's a real PITA for dynamic code.

I actually tested this. I was surprised. In D2, variables are allowed...


But not dynamic ones

It is a surprise though.
What do you mean 'dynamic ones' ?
Eg, this works:

for (int i=0; i<10; ++i) {
switch(x) {
case 3:
break;
case i:
break;
default:
}
}

Oop. Never mind. I was looking at

void main(string[] args){
  int i = to!(int)(args[1]);
  switch(i){
    case args.length: writeln("hey!");
  }
}

Now I'm really surprised.


The spec says:
"The case expressions must all evaluate to a constant value or array, or a runtime initialized const or immutable variable of integral type. "

But in reality, it seems that the cases can either be int or string symbols or expressions. If they are expressions, they are evaluated at compile time. That's quite nice, as it avoids the downsides which BCS and I had mentioned. Maybe it's too much of a special case though? Seems like yet another Easter egg. Or else it's a bug.

Reply via email to