switch()

Steven Schveighoffer schveiguy at yahoo.com
Sun Feb 16 12:58:55 PST 2014


On Sun, 16 Feb 2014 11:08:51 -0500, Manu <turkeyman at gmail.com> wrote:

> Another thing that I often do is switch on an expression, but I often  
> have
> a problem where, within the scope, I have no way to refer to the result
> value.
> This is particularly important when range cases appear.
>
> switch(x+10)
> {
>   case 10: .. case 20:
>     // what is the value?
> }
>
> Obviously then I need to do this:
>
> auto y = x+10;
> switch(y)
> {
>   case 10: .. case 20:
>     // I know y...
> }
>
> But this is a bit lame. I'm polluting the outer namespace, and wasting a
> line.
>
> I wonder if a variable declaration could be made possible in the switch:
>
> switch(y = x+10) // obviously implicitly auto, like in foreach
> {
>   case 10: .. case 20:
>     // I have y, no pollution of the outer scope, no wasted line. yay!
> }
>
> for and foreach can both declare variables this way... does it make sense
> here? Useful?

Yes, this would be good. I bet bearophile has an enhancement request  
already ;)

-Steve


More information about the Digitalmars-d mailing list