Improvement to switch-case statement

Benji Smith dlanguage at benjismith.net
Fri Jan 2 18:30:07 PST 2009


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



More information about the Digitalmars-d mailing list