Skip to content

Commit 39b4674

Browse files
committed
[statement.dd] Tweak some examples & make them runnable
Move subheading about foreach/break/continue into main foreach section. Fix using `size_t` for foreach index type. Fix wrong heading level for *Foreach Range Statement*. Qualify `with` equivalency example: 'if `ident` is a member of the type of `expression`'. Fix and simplify mixin statement example.
1 parent 53cbbf0 commit 39b4674

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

spec/statement.dd

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ $(P
501501
in the $(PS0).
502502
)
503503

504+
$(P A $(GLINK BreakStatement) in the body of the foreach will exit the
505+
foreach, a $(GLINK ContinueStatement) will immediately start the
506+
next iteration.
507+
)
508+
504509
$(H3 $(LNAME2 foreach_over_arrays, Foreach over Arrays))
505510

506511
$(P
@@ -518,14 +523,17 @@ $(P
518523
$(I index) cannot be `ref`.
519524
It is set to be the index of the array element.
520525
)
526+
$(SPEC_RUNNABLE_EXAMPLE_RUN
521527
--------------
522-
char[] a;
523-
...
524-
foreach (int i, char c; a)
528+
import std.stdio;
529+
char[] a = ['h', 'i'];
530+
531+
foreach (size_t i, char c; a)
525532
{
526533
writefln("a[%d] = '%c'", i, c);
527534
}
528535
--------------
536+
)
529537

530538
$(P For $(D foreach), the
531539
elements for the array are iterated over starting at index 0
@@ -559,7 +567,9 @@ $(H3 $(LNAME2 foreach_over_arrays_of_characters, Foreach over Arrays of Characte
559567
can be decoded into any UTF type:
560568
)
561569

570+
$(SPEC_RUNNABLE_EXAMPLE_RUN
562571
--------------
572+
import std.stdio;
563573
char[] a = "\xE2\x89\xA0".dup; // \u2260 encoded as 3 UTF-8 bytes
564574

565575
foreach (dchar c; a)
@@ -574,15 +584,16 @@ foreach (char c; b)
574584
writef("%x, ", c); // prints 'e2, 89, a0, '
575585
}
576586
--------------
577-
587+
)
578588

579589
$(P Aggregates can be string literals, which can be accessed
580590
as char, wchar, or dchar arrays:
581591
)
582592

583-
--------------
584-
void test()
585-
{
593+
$(SPEC_RUNNABLE_EXAMPLE_RUN
594+
--------------
595+
import std.stdio;
596+
586597
foreach (char c; "ab")
587598
{
588599
writefln("'%s'", c);
@@ -591,8 +602,8 @@ void test()
591602
{
592603
writefln("'%s'", w);
593604
}
594-
}
595-
--------------
605+
--------------
606+
)
596607

597608
$(P which would print:
598609
)
@@ -665,6 +676,7 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
665676

666677
$(P For example, consider a class that is a container for two elements:)
667678

679+
$(SPEC_RUNNABLE_EXAMPLE_RUN
668680
--------------
669681
class Foo
670682
{
@@ -674,35 +686,33 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
674686
{
675687
int result = 0;
676688

677-
for (int i = 0; i < array.length; i++)
689+
foreach (e; array)
678690
{
679-
result = dg(array[i]);
691+
result = dg(e);
680692
if (result)
681693
break;
682694
}
683695
return result;
684696
}
685697
}
686-
--------------
687-
688-
$(P An example using this might be:)
689698

690-
--------------
691-
void test()
699+
void main()
692700
{
701+
import std.stdio;
693702
Foo a = new Foo();
694703

695704
a.array[0] = 73;
696705
a.array[1] = 82;
697706

698707
foreach (uint u; a)
699708
{
700-
writefln("%d", u);
709+
writeln(u);
701710
}
702711
}
703712
--------------
713+
)
704714

705-
$(P which would print:)
715+
$(P This would print:)
706716

707717
$(CONSOLE
708718
73
@@ -926,6 +936,8 @@ $(P
926936
and it is set to the index of each sequence element.
927937
)
928938
$(P Example:)
939+
940+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
929941
-----
930942
import std.meta : AliasSeq;
931943

@@ -939,6 +951,7 @@ void main()
939951
}
940952
}
941953
-----
954+
)
942955
$(P Output:)
943956

944957
$(CONSOLE
@@ -954,21 +967,21 @@ $(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
954967
$(P $(D ref) can be used to update the original elements:
955968
)
956969

957-
--------------
958-
void test()
959-
{
960-
static uint[2] a = [7, 8];
970+
$(SPEC_RUNNABLE_EXAMPLE_RUN
971+
--------------
972+
import std.stdio;
973+
uint[2] a = [7, 8];
961974

962975
foreach (ref uint u; a)
963976
{
964977
u++;
965978
}
966979
foreach (uint u; a)
967980
{
968-
writefln("%d", u);
981+
writeln(u);
969982
}
970-
}
971-
--------------
983+
--------------
984+
)
972985

973986
which would print:
974987

@@ -1002,7 +1015,7 @@ foreach (int i; a)
10021015
a = null; // ok
10031016
--------------
10041017

1005-
$(H2 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
1018+
$(H3 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
10061019

10071020
$(P A foreach range statement loops over the specified range.)
10081021

@@ -1036,6 +1049,7 @@ $(GNAME ForeachRangeStatement):
10361049
is executed.
10371050
)
10381051

1052+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10391053
---
10401054
import std.stdio;
10411055

@@ -1053,6 +1067,7 @@ void main()
10531067
}
10541068
}
10551069
---
1070+
)
10561071

10571072
prints:
10581073

@@ -1061,14 +1076,6 @@ foo0123456789
10611076
)
10621077

10631078

1064-
$(H3 $(LNAME2 break_and_continue_out_of_foreach, Break and Continue out of Foreach))
1065-
1066-
1067-
$(P A $(GLINK BreakStatement) in the body of the foreach will exit the
1068-
foreach, a $(GLINK ContinueStatement) will immediately start the
1069-
next iteration.
1070-
)
1071-
10721079
$(H2 $(LEGACY_LNAME2 SwitchStatement, switch-statement, Switch Statement))
10731080

10741081
A switch statement goes to one of a collection of case
@@ -1443,7 +1450,7 @@ $(GNAME WithStatement):
14431450
Within the with body the referenced object is searched first for
14441451
identifier symbols.
14451452

1446-
$(P The $(I WithStatement))
1453+
$(P Below, if `ident` is a member of the type of `expression`, the $(I WithStatement):)
14471454

14481455
--------------
14491456
with (expression)
@@ -1487,7 +1494,7 @@ with (Foo)
14871494
}
14881495
--------------
14891496

1490-
$(P Use of with object symbols that shadow local symbols with
1497+
$(P Use of `with` object symbols that shadow local symbols with
14911498
the same identifier are not allowed.
14921499
This is to reduce the risk of inadvertent breakage of with
14931500
statements when new members are added to the object declaration.
@@ -1512,6 +1519,8 @@ void main()
15121519
$(P In nested $(I WithStatement)s, the inner-most scope takes precedence. If
15131520
a symbol cannot be resolved at the inner-most scope, resolution is forwarded
15141521
incrementally up the scope hierarchy.)
1522+
1523+
$(SPEC_RUNNABLE_EXAMPLE_RUN
15151524
---
15161525
import std.stdio;
15171526

@@ -1571,8 +1580,8 @@ void main()
15711580
// subsequently forward to module scope.
15721581
}
15731582
}
1574-
15751583
---
1584+
)
15761585

15771586
$(H2 $(LEGACY_LNAME2 SynchronizedStatement, synchronized-statement, Synchronized Statement))
15781587

@@ -1684,6 +1693,7 @@ $(GNAME FinallyStatement):
16841693
to the original exception (the head of the chain) if a bypass occurred,
16851694
so that the entire exception history is retained.)
16861695

1696+
$(SPEC_RUNNABLE_EXAMPLE_RUN
16871697
--------------
16881698
import std.stdio;
16891699

@@ -1709,6 +1719,7 @@ int main()
17091719
return 0;
17101720
}
17111721
--------------
1722+
)
17121723

17131724
prints:
17141725

@@ -1770,8 +1781,10 @@ $(PSCURLYSCOPE) when the scope exits due to exception unwinding.
17701781
scope, their destructions will be interleaved with the $(I ScopeGuardStatement)s
17711782
in the reverse lexical order in which they appear.)
17721783

1773-
1784+
$(SPEC_RUNNABLE_EXAMPLE_RUN
17741785
----
1786+
import std.stdio;
1787+
17751788
write("1");
17761789
{
17771790
write("2");
@@ -1781,6 +1794,7 @@ write("1");
17811794
}
17821795
writeln();
17831796
----
1797+
)
17841798

17851799
writes:
17861800

@@ -1789,7 +1803,9 @@ $(CONSOLE
17891803
)
17901804

17911805

1806+
$(SPEC_RUNNABLE_EXAMPLE_RUN
17921807
----
1808+
import std.stdio;
17931809
{
17941810
scope(exit) write("1");
17951811
scope(success) write("2");
@@ -1798,15 +1814,18 @@ $(CONSOLE
17981814
}
17991815
writeln();
18001816
----
1817+
)
18011818

18021819
writes:
18031820

18041821
$(CONSOLE
18051822
4321
18061823
)
18071824

1808-
1825+
$(SPEC_RUNNABLE_EXAMPLE_RUN
18091826
----
1827+
import std.stdio;
1828+
18101829
struct Foo
18111830
{
18121831
this(string s) { write(s); }
@@ -1829,6 +1848,7 @@ catch (Exception e)
18291848
}
18301849
writeln();
18311850
----
1851+
)
18321852

18331853
writes:
18341854

@@ -1949,32 +1969,32 @@ $(GNAME MixinStatement):
19491969
$(GLINK StatementList), and is compiled as such.
19501970
)
19511971

1972+
$(SPEC_RUNNABLE_EXAMPLE_RUN
19521973
---
19531974
import std.stdio;
19541975

19551976
void main()
19561977
{
1957-
int j;
1978+
int i = 0;
19581979
mixin("
19591980
int x = 3;
1960-
for (int i = 0; i < 3; i++)
1961-
writeln(x + i, ++j);
1981+
for (; i < 3; i++)
1982+
writeln(x + i, i);
19621983
"); // ok
19631984

1964-
string s = "int y;";
1985+
enum s = "int y;";
19651986
mixin(s); // ok
19661987
y = 4; // ok, mixin declared y
19671988

19681989
string t = "y = 3;";
1969-
mixin(t); // error, t is not evaluatable at compile time
1970-
1971-
mixin("y =") 4; // error, string must be complete statement
1990+
//mixin(t); // error, t is not evaluatable at compile time
1991+
//mixin("y =") 4; // error, string must be complete statement
19721992

19731993
mixin("y =" ~ "4;"); // ok
1974-
19751994
mixin("y =", 2+2, ";"); // ok
19761995
}
19771996
---
1997+
)
19781998

19791999
$(SPEC_SUBNAV_PREV_NEXT expression, Expressions, arrays, Arrays)
19802000
)

0 commit comments

Comments
 (0)