case statement allows for runtime values,

Jesse Phillips jessekphillips+D at gmail.com
Tue Apr 19 14:11:33 PDT 2011


Andrej Mitrovic Wrote:

> Got it. Bug is reported.
> 
> Btw, is there a specific reason why non-const values are not allowed?
> 
> I mean, doesn't a switch statement like this:
> switch(value)
> {
>     case 1:
>         foo(); break;
>     case 2:
>         bar(); break;
>     default:
>         doo();
> }
> 
> expand to:
> 
> if (value == 1)
>     foo();
> else if (value == 2)
>     bar();
> else
>     doo();
> 
> You can compare anything in an if statement, so why is switch more limited?

No, it doesn't lower to an if/else statement. I didn't quite understand the details, but I'm actually pretty sure it needs a compile-time value.


More information about the Digitalmars-d-learn mailing list