Skip to content

Commit f5a3ca8

Browse files
authored
Merge pull request #3179 from ntrel/switch-docs
[spec/statement.dd] Improve switch docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents e65a242 + a8974f4 commit f5a3ca8

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

spec/statement.dd

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,36 +1124,33 @@ $(GNAME StatementNoCaseNoDefault):
11241124
a match, the corresponding case statement is transferred to.
11251125
)
11261126

1127-
$(P The case expressions, $(GLINK2 expression, ArgumentList),
1127+
$(P The case expressions in $(I ArgumentList)
11281128
are a comma separated list of expressions.
11291129
)
11301130

11311131
$(P A $(I CaseRangeStatement) is a shorthand for listing a series
11321132
of case statements from $(I FirstExp) to $(I LastExp).
11331133
)
11341134

1135-
1136-
$(P If none of the case expressions match, and there is a default
1137-
statement, the default statement is transferred to.
1138-
)
1139-
1140-
1141-
$(P A switch statement must have a default statement.)
1142-
1143-
11441135
$(P The case expressions must all evaluate to a constant value or array,
11451136
or a runtime initialized const or immutable variable of integral type.
11461137
They must be implicitly convertible to the type of the switch
11471138
*Expression*. )
11481139

11491140
$(P Case expressions must all evaluate to distinct values. Const or
11501141
immutable variables must all have different names. If they share a
1151-
value, the first case statement with that value gets control. There must
1152-
be exactly one default statement.)
1142+
value, the first case statement with that value gets control.)
11531143

11541144
$(P The $(GLINK ScopeStatementList) introduces a new scope.
11551145
)
11561146

1147+
$(P A `break` statement will exit the switch $(I BlockStatement).)
1148+
1149+
$(P A switch statement must have exactly one *DefaultStatement*.
1150+
If none of the case expressions match, control is transferred
1151+
to the default statement.
1152+
)
1153+
11571154
$(P Case statements and default statements associated with the switch
11581155
can be nested within block statements; they do not have to be in
11591156
the outermost block. For example, this is allowed:
@@ -1166,53 +1163,70 @@ switch (i)
11661163
{
11671164
case 2:
11681165
}
1166+
i++;
11691167
break;
1168+
default:
11701169
}
11711170
--------------
11721171

1172+
$(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
11731173

11741174

11751175
$(P A $(GLINK ScopeStatementList) must either be empty, or be ended with
11761176
a $(GLINK ContinueStatement), $(GLINK BreakStatement),
11771177
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK ThrowStatement)
1178-
or assert(0) expression unless this is the last case. This is to
1179-
set apart with C's error-prone implicit fall-through behavior.
1180-
$(D goto case;) could be used for explicit fall-through:
1181-
)
1178+
or `assert(0)` expression unless this is the last case. This is to
1179+
set apart with C's error-prone implicit fall-through behavior.)
11821180

11831181
--------------
1184-
int number;
1185-
string message;
1186-
switch (number)
1182+
switch (i)
11871183
{
1188-
default: // valid: ends with 'throw'
1189-
throw new Exception("unknown number");
1184+
case 1:
1185+
message ~= "one";
1186+
// ERROR: implicit fall-through
1187+
case 2:
1188+
// valid: the body is empty
1189+
default:
1190+
message ~= "unknown";
1191+
}
1192+
--------------
11901193

1191-
case 3: // valid: ends with 'break' (break out of the 'switch' only)
1192-
message ~= "three ";
1193-
break;
1194+
$(P $(D goto case;) can be used for explicit fall-through:)
11941195

1195-
case 4: // valid: ends with 'continue' (continue the enclosing loop)
1196-
message ~= "four ";
1197-
continue;
1196+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1197+
--------------
1198+
string message;
1199+
foreach (i; 1..5)
1200+
{
1201+
switch (i)
1202+
{
1203+
default: // valid: ends with 'throw'
1204+
throw new Exception("unknown number");
11981205

1199-
case 5: // valid: ends with 'goto' (explicit fall-through to next case.)
1200-
message ~= "five ";
1201-
goto case;
1206+
case 3: // valid: ends with 'break' (break out of the 'switch' only)
1207+
message ~= "three";
1208+
break;
1209+
1210+
case 4: // valid: ends with 'continue' (continue the enclosing loop)
1211+
message ~= "four";
1212+
continue; // don't append a comma
12021213

1203-
case 6: // ERROR: implicit fall-through
1204-
message ~= "six ";
1214+
case 1: // valid: ends with 'goto' (explicit fall-through to next case.)
1215+
message ~= ">";
1216+
goto case;
12051217

1206-
case 1: // valid: the body is empty
1207-
case 2: // valid: this is the last case in the switch statement.
1208-
message = "one or two";
1218+
case 2: // valid: this is the last case in the switch statement.
1219+
message ~= "one or two";
1220+
}
1221+
message ~= ", ";
12091222
}
1223+
writeln(message);
12101224
--------------
1225+
)
12111226

1212-
$(P A break statement will exit the switch $(I BlockStatement).)
1213-
1227+
$(H3 $(LNAME2 string-switch, String Switch))
12141228

1215-
$(P $(LNAME2 string-switch, Strings can be used in switch expressions).
1229+
$(P Strings can be used in switch expressions.
12161230
For example:
12171231
)
12181232

0 commit comments

Comments
 (0)