Improvement to switch-case statement

Yigal Chripun yigal100 at gmail.com
Fri Jan 2 15:36:23 PST 2009


bearophile wrote:
> 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

Sorry, I guess I wasn't clear. I meant to say: remove the *current* 
syntax of the trenary operator. making "if" an expression is one 
possible solution to this, as you noted above.

Maybe it's just me but all those C-style statements seem so arcane and 
unnessaccary. real OOP languages do not need control structures to be 
part of the language - they're part of the class library instead.
Here's some Smalltalk examples: (and D-like comparable code)

10 times: [ code ]. // 10.times({ code to be repeated });
map each: [ code ]. // map.each((Type val){ code to be repeated })
(var > 5) ifTrue: [] ifFalse: [] // (var > 5).IfTrueFalse(dgTrue, dgFalse);

// reverse the order with a different method of boolean objects
var (var > 5) ifFalse: [] ifTrue: []

// use just one of the branches:
var (var < 6) ifFalse: []

I guess D is unlikely to adopt the above...




More information about the Digitalmars-d mailing list