CaseStatement specification issue

Baz via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 12:39:49 PDT 2014


On Thursday, 14 August 2014 at 17:10:50 UTC, AsmMan wrote:
> On Thursday, 14 August 2014 at 16:01:05 UTC, Baz wrote:
>> On Thursday, 14 August 2014 at 13:19:36 UTC, Sergey Kozyr 
>> wrote:
>>> Sorry for previous message. Once again:
>>>
>>> I was reading D language reference and found some issue with
>>> "case ... :" statement specifications.
>>> Documentation http://dlang.org/statement#CaseStatement  says
>>> that after
>>>
>>> case 1:
>>>
>>> must be non-empty statement "ScopeStatementList". For example
>>>
>>> case 1:
>>> return true;
>>>
>>> case 2: {
>>> }
>>>
>>> case 3: ;
>>>
>>> But next example concerns me:
>>>
>>> case 1:
>>> case 2:
>>> return true;
>>> case 3:
>>> return false;
>>>
>>> After "case 1:" we see no statement at all. This conflicts 
>>> with "CaseStatement" rule of documentation. But this is right 
>>> D code.
>>> I think language reference must be updated.
>>
>> It's a "fall trough":
>>
>> https://en.wikipedia.org/wiki/Switch_statement#Fallthrough
>
> It's a fall trough but it is not the reason why it does works. 
> It does because (as Daniel already mentioned) a case is a valid 
> statement, therefore a case following other case is a totally 
> valid statement.
>
> for example:
>
> case 1:
> case 2:
>   return true;
>
> There are three statements. One which must be followed by 
> another (case keyword) and another by an expression (return 
> keyword).
>
> It is not even a special-case in the switch in C language. The 
> switch keyword accept a expression constant as argument and 
> must be followed by a statement, so:
>
> switch(1) return 0;
>
> is valid C code and no standard-compliant compiler must give an 
> error about that. The fact we can do:
>
> switch(E)
> {
>    case 1: ... break;
>    case 2: .... break;
> default: ... break;
> }
>
> That's why compound-statement is a statement too (which switch 
> does accept same reason why break keyword is needed)

I totally agree. I just wanted to mention the technical name.



More information about the Digitalmars-d mailing list