Skip to content

Commit 81cf49a

Browse files
authored
Merge pull request #3573 from ntrel/switch
SwitchStatement: Improve docs Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Signed-off-by: Nicholas Wilson <thewilsonator@users.noreply.github.com> Merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents f7574e7 + 3551dac commit 81cf49a

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

spec/statement.dd

Lines changed: 59 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)
@@ -1372,6 +1398,8 @@ foreach (i; 1..5)
13721398
writeln(message);
13731399
--------------
13741400
)
1401+
$(P $(RELATIVE_LINK2 goto-statement, `goto`) also supports jumping to
1402+
a specific case or the default case statement.)
13751403

13761404
$(H3 $(LNAME2 string-switch, String Switch))
13771405

@@ -1392,10 +1420,10 @@ switch (name)
13921420

13931421
$(P For applications like command line switch processing, this
13941422
can lead to much more straightforward code, being clearer and
1395-
less error prone. char, wchar and dchar strings are allowed.
1423+
less error prone. `char`, `wchar` and `dchar` strings are allowed.
13961424
)
13971425

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

0 commit comments

Comments
 (0)