"Steven Schveighoffer" <schvei...@yahoo.com> wrote in message 
news:op.u4hhjisgeav...@localhost.localdomain...
> On Sat, 05 Dec 2009 16:08:00 -0500, Nick Sabalausky <a...@a.a> 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.
>
> you mean a real PITA to use if instead of switch?

For long "if else(x == y)" chains where the x is always the same? Yes, 
absolutely.

> AFAIK, switch is the  way it is to foster different optimizations by the 
> compiler.

If the only reason for switch were optimization, then switch would be 
useless because it would be easy for the compiler to detect:

if(x == constVal1) {}
else if(x == constVal2) {}
// Etc

And do the exact same optimizations.

> I don't know  if optimizations can be performed if the cases are dynamic.

Compared to the if-else chains? Probably not. But so what?

> Also, what  happens if two of the cases are dynamically the same?

Switch is a shorthand for a certain pattern of if-else chains, as such, it 
should behave the same by choosing the first and skipping the rest.

> I think one of the  optimizations is that the compiler can reorder the 
> cases to make the  testing more streamlined.
>

Ok, so it just won't do that with non-constant cases.


Reply via email to