@@ -1227,15 +1227,6 @@ $(GNAME SwitchStatement):
1227
1227
$(GNAME CaseStatement):
1228
1228
$(D case) $(GLINK2 expression, ArgumentList) $(D :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
1229
1229
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
-
1239
1230
$(GNAME DefaultStatement):
1240
1231
$(D default :) $(PSSEMI_PSCURLYSCOPE_LIST)$(OPT)
1241
1232
@@ -1267,28 +1258,15 @@ $(GNAME StatementNoCaseNoDefault):
1267
1258
)
1268
1259
1269
1260
$(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,
1286
1263
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*.)
1289
1266
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
1292
1270
value, the first case statement with that value gets control.)
1293
1271
1294
1272
$(P The $(GLINK ScopeStatementList) introduces a new scope.
@@ -1301,6 +1279,27 @@ $(GNAME StatementNoCaseNoDefault):
1301
1279
to the default statement.
1302
1280
)
1303
1281
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
+ )
1304
1303
$(P Case statements and default statements associated with the switch
1305
1304
can be nested within block statements; they do not have to be in
1306
1305
the outermost block. For example, this is allowed:
@@ -1319,6 +1318,32 @@ switch (i)
1319
1318
}
1320
1319
--------------
1321
1320
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
+
1322
1347
$(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
1323
1348
1324
1349
@@ -1345,6 +1370,7 @@ switch (i)
1345
1370
$(SPEC_RUNNABLE_EXAMPLE_RUN
1346
1371
--------------
1347
1372
string message;
1373
+
1348
1374
foreach (i; 1..5)
1349
1375
{
1350
1376
switch (i)
@@ -1392,10 +1418,10 @@ switch (name)
1392
1418
1393
1419
$(P For applications like command line switch processing, this
1394
1420
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.
1396
1422
)
1397
1423
1398
- $(P $(D Implementation Note:) The compiler's code generator may
1424
+ $(P $(B Implementation Note:) The compiler's code generator may
1399
1425
assume that the case
1400
1426
statements are sorted by frequency of use, with the most frequent
1401
1427
appearing first and the least frequent last. Although this is
0 commit comments