Improvement to switch-case statement

Nick Sabalausky a at a.a
Sat Jan 3 06:00:20 PST 2009


"Benji Smith" <dlanguage at benjismith.net> wrote in message 
news:gjmikc$vos$1 at digitalmars.com...
> Yigal Chripun wrote:
>> also, some thought should be spent on getting rid of the ternary op 
>> syntax since it interferes with other things that could be added to the 
>> language (nullable types, for instance)
>
> Heresy!
>
> The ternary operator is one of my favorite tools. If you want to get rid 
> of it, I think you'd have to make the 'if' statement into an expression 
> (which would open up a whole other can of worms).
>
> As I showed earlier, there's no ambiguity between the ternary operator and 
> the nullable type suffix. The ambiguity comes from the case statement. In 
> my opinion, the best way to resolve that ambiguity is to add braces around 
> case statments, like this:
>
>   switch (x) {
>     case 1 { ... }
>     case 2 { ... }
>     default { ... }
>   }
>
> But that might make it impossible to implement Duff's Device (blessing or 
> curse? personally, I don't care).
>
> And it might imply the creation of a new scope with each case. Currently, 
> a case statement doesn't introduce its own lexical scope.
>
> Anyhoo... Don't mess with the ternary operator!!
>
> :)
>
> --benji

Right. I use ?: constantly. I can't stand having my code littered with this 
sort of obfuscated clutter...

if(someFlag)
   a = 0;
else
   a = b;  // Also note, the lvalue is non-DRY

char[] outStr;
if(a == 0)
   outStr = "(zero)";
else
   outStr = "(non-zero)";

Stdout.formatln("blah blah blah {}", outStr);

...when I could just do a nice, neat, clear (can tell at a glance what's 
going on):

a = someFlag? 0 : b;
Stdout.formatln("blah blah blah {}",
                        a==0? "zero" : "non-zero"); 





More information about the Digitalmars-d mailing list