Skip to content

Commit ae13dbe

Browse files
committed
SwitchStatement: Improve docs
Move CaseRangeStatement to its own subheading. Tighten wording on case expression values. Add rationale for requiring default, mention `final switch`. Add general example.
1 parent d6933ba commit ae13dbe

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

spec/statement.dd

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,15 +1227,6 @@ $(GNAME SwitchStatement):
12271227
$(GNAME CaseStatement):
12281228
$(D case) $(GLINK2 expression, ArgumentList) $(D :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
12291229

1230-
$(GNAME CaseRangeStatement):
1231-
$(D case) $(GLINK FirstExp) $(D : .. case) $(GLINK LastExp) $(D :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
1232-
1233-
$(GNAME FirstExp):
1234-
$(ASSIGNEXPRESSION)
1235-
1236-
$(GNAME LastExp):
1237-
$(ASSIGNEXPRESSION)
1238-
12391230
$(GNAME DefaultStatement):
12401231
$(D default :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
12411232

@@ -1267,28 +1258,15 @@ $(GNAME StatementNoCaseNoDefault):
12671258
)
12681259

12691260
$(P The case expressions in $(I ArgumentList)
1270-
are a comma separated list of expressions.)
1271-
1272-
---
1273-
case 1, 2, 3:
1274-
---
1275-
1276-
$(P A $(I CaseRangeStatement) is a shorthand for listing a series
1277-
of case statements from $(I FirstExp) to $(I LastExp), inclusive.)
1278-
1279-
---
1280-
case 1: .. case 3:
1281-
---
1282-
1283-
$(P The two examples above are equivalent.)
1284-
1285-
$(P The case expressions must all evaluate to a constant value or array,
1261+
are a comma separated list of expressions.
1262+
Each expression must evaluate to a compile-time value or array,
12861263
or a runtime initialized const or immutable variable of integral type.
1287-
They must be implicitly convertible to the type of the switch
1288-
*Expression*. )
1264+
Each expression must be implicitly convertible to the type of the switch
1265+
*Expression*.)
12891266

1290-
$(P Case expressions must all evaluate to distinct values. Const or
1291-
immutable variables must all have different names. If they share a
1267+
$(P Compile-time case values must all be distinct. Const or
1268+
immutable runtime variables must all have different names.
1269+
If two case expressions share a
12921270
value, the first case statement with that value gets control.)
12931271

12941272
$(P The $(GLINK ScopeStatementList) introduces a new scope.
@@ -1301,6 +1279,27 @@ $(GNAME StatementNoCaseNoDefault):
13011279
to the default statement.
13021280
)
13031281

1282+
$(RATIONALE This makes it clear that all possible cases are intentionally handled.
1283+
See also: $(RELATIVE_LINK2 final-switch-statement, `final switch`).)
1284+
1285+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1286+
--------------
1287+
foreach (i; 2 .. 10)
1288+
{
1289+
bool prime;
1290+
1291+
switch (i)
1292+
{
1293+
case 2, 3, 5, 7:
1294+
prime = true;
1295+
break;
1296+
default:
1297+
prime = false;
1298+
}
1299+
writeln(i, ": ", prime);
1300+
}
1301+
--------------
1302+
)
13041303
$(P Case statements and default statements associated with the switch
13051304
can be nested within block statements; they do not have to be in
13061305
the outermost block. For example, this is allowed:
@@ -1319,6 +1318,32 @@ switch (i)
13191318
}
13201319
--------------
13211320

1321+
$(H3 $(LNAME2 case-range, Case Range Statement))
1322+
1323+
$(GRAMMAR
1324+
$(GNAME CaseRangeStatement):
1325+
$(D case) $(GLINK FirstExp) $(D : .. case) $(GLINK LastExp) $(D :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
1326+
1327+
$(GNAME FirstExp):
1328+
$(ASSIGNEXPRESSION)
1329+
1330+
$(GNAME LastExp):
1331+
$(ASSIGNEXPRESSION)
1332+
)
1333+
1334+
$(P A $(I CaseRangeStatement) is a shorthand for listing a series
1335+
of case statements from $(I FirstExp) to $(I LastExp), inclusive.)
1336+
1337+
---
1338+
case 1: .. case 3:
1339+
---
1340+
1341+
$(P The above is equivalent to:)
1342+
1343+
---
1344+
case 1, 2, 3:
1345+
---
1346+
13221347
$(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
13231348

13241349

@@ -1345,6 +1370,7 @@ switch (i)
13451370
$(SPEC_RUNNABLE_EXAMPLE_RUN
13461371
--------------
13471372
string message;
1373+
13481374
foreach (i; 1..5)
13491375
{
13501376
switch (i)
@@ -1392,10 +1418,10 @@ switch (name)
13921418

13931419
$(P For applications like command line switch processing, this
13941420
can lead to much more straightforward code, being clearer and
1395-
less error prone. char, wchar and dchar strings are allowed.
1421+
less error prone. `char`, `wchar` and `dchar` strings are allowed.
13961422
)
13971423

1398-
$(P $(D Implementation Note:) The compiler's code generator may
1424+
$(P $(B Implementation Note:) The compiler's code generator may
13991425
assume that the case
14001426
statements are sorted by frequency of use, with the most frequent
14011427
appearing first and the least frequent last. Although this is

0 commit comments

Comments
 (0)