Improvement to switch-case statement

bearophile bearophileHUGS at lycos.com
Fri Jan 2 13:49:59 PST 2009


Yigal Chripun:
> 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)

But a ternary operator is sometimes handy: when used judiciously it may help the mind of the person that reads the code to form a "chunk", improving code readability a little.

And if you want to modify/remove it you have to remember that D is usually designed to act as C when a C syntax is accepted (even if this leads to some bad things, see the syntax of the "switch" statement or the casting mess).

There are many possible alternative syntaxes for the ternary operator. Python in the end has accepted this one:
z = 1 if x else y
That for example can be used like this:
print n, "item" + ("" if a == 1 else "s")

Bye,
bearophile



More information about the Digitalmars-d mailing list