request switch statement with common block

JS js.mdnq at gmail.com
Sat Aug 3 13:06:59 PDT 2013


On Saturday, 3 August 2013 at 19:22:53 UTC, Walter Bright wrote:
> On 8/3/2013 12:00 PM, JS wrote:
>> What I really don't get it is why people think that just 
>> because they won't use
>> such a feature then it must be useless to everyone else.
>
> You could provide supporting evidence by examining every use of 
> switch in the dmd, phobos, and druntime source code, and see 
> what percentage of those would benefit from your proposal.


This is illogical. The code wasn't written with such a semantic 
in mind so it would require me to rewrite the library to see how 
useful it truly is. The semantic should stand on it's own. Either 
it extends some basic functionality in a good way or it doesn't. 
i.e., Does it add value? If it does, then it should be 
implemented assuming the implementation costs and maintenance is 
not high. In this case the implementation costs and maintenance 
is near constant(since it is an orthogonal semantic not impacting 
any other aspect in the compiler but the switch statement itself).

In fact, if the D compiler had some ability to easily implement 
transformations then one could simply rewrite the switch 
statement into what it represents.

Such transformations depend only on the local context(what you 
are immediately transforming) and simply rewrite the code into 
other code.

e.g.,

compiler see's

switch (cond)
{
     <common>: <common_body1> <body_end_statement>;
     case <symbol> : <case body> [break or return or fallthrough]
     ...
     <default> : <default_body>  [break or return or fallthrough]
     <common>: <common_body2> <body_end_statement>;
} else <else_body>

AND rewrites it to

if (cond) { <common_body1> }
switch (cond)
{
     case <symbol> : <case body> [break or return or fallthrough]
     ...
     <default> : <default_body>  [break or return or fallthrough]
}
if (cond) { <common_body2> }
if (!cond) { <else_body> }


The second is already valid D. The first is a "short hand" which 
is less error prone and less verbose. Since the transformation is 
rather trivial and, in a sense, "linear", it is very low impact 
to the maintenance and implementation of the compiler.

What it offers? Nothing to those that don't use it... everything 
to those that do.

> Consider, for example, the scope guard statement in D. It is 
> extremely useful - but it is an unusual form (doesn't exist in 
> other languages) and programmers just don't think in those 
> terms. Andrei & I constantly have to work at 'selling' the 
> benefits of it. It still hasn't really caught on.

And what I'm asking for is similar to scope guards for switch 
statements. You see how difficult is for me to "sell" it.

But you can't design a language for the lowest common 
denominator. The best language available is the one that can do 
it all for everyone. With such a language everything else will 
follow. Having features that no one will use is not necessary 
bad... at some point chances are someone will want that feature 
and use it.

For example, suppose what I am suggesting has zero cost to 
implement and maintain...

Would you allow it in D?

If not, why not? What are the drawbacks to having it? Is it error 
prone? Completely useless(Obviously not, because at least 2 
people would find it useful)? etc...



More information about the Digitalmars-d mailing list