Few things

Don Clugston dac at nospam.com.au
Mon Aug 6 01:19:51 PDT 2007


Chris Nicholson-Sauls wrote:
> Don Clugston wrote:
>> Lionello Lunesu wrote:
>>>> 7) From the FAQ: 
>>>
>>>> Many people have asked for a requirement that there be a break
>>>  > between cases in a switch statement, that C's behavior of
>>>  > silently falling through is the cause of many bugs.
>>>  > The reason D doesn't change this is for the same reason that
>>>  > integral promotion rules and operator precedence rules were
>>>  > kept the same - to make code that looks the same as in C
>>>  > operate the same. If it had subtly different semantics, it
>>>  > will cause frustratingly subtle bugs.
>>>
>>>> I agree with both points of view. My idea: calling this
>>>  > statement differently (like caseof) instead of "switch"
>>>  > (like in Pascal), so you can change its semantics too,
>>>  > removing the falling through (you may use the Pascal
>>>  > semantic too).
>>>
>>> Sorry to hijack your point here, but this got me thinking:
>>>
>>> Why not use "continue" for seeping through to the next case 
>>> statement? DMD could then complain if a case does not end with 
>>> break/continue/goto/return and silently insert a assert(0) before 
>>> each case (the way it does for functions that return a value.)
>>
>> Doesn't
>>
>> for (c;;) {
>>   switch (c) {
>>   case 'a': continue;
>>   case 'b': break;
>>   }
>> }
>>
>> already have a meaning?
> 
> I can't think of any other case where this problem would show (aside 
> from other looping statements, so the fix is the same).  Since this is 
> probably not an overly common case (I've done it myself, but not very 
> often), I don't think writing a label on the loop would be a big deal.

That's fine. The issue is that it would silently break existing code.



More information about the Digitalmars-d mailing list