Skip to content

Commit e9edd87

Browse files
authored
Merge pull request #3166 from ntrel/statement-ex
[statement.dd] Improve docs & examples Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents eb0901f + 481ce69 commit e9edd87

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

spec/statement.dd

Lines changed: 57 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,16 @@ $(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+
char[] a = ['h', 'i'];
529+
530+
foreach (size_t i, char c; a)
525531
{
526532
writefln("a[%d] = '%c'", i, c);
527533
}
528534
--------------
535+
)
529536

530537
$(P For $(D foreach), the
531538
elements for the array are iterated over starting at index 0
@@ -559,6 +566,7 @@ $(H3 $(LNAME2 foreach_over_arrays_of_characters, Foreach over Arrays of Characte
559566
can be decoded into any UTF type:
560567
)
561568

569+
$(SPEC_RUNNABLE_EXAMPLE_RUN
562570
--------------
563571
char[] a = "\xE2\x89\xA0".dup; // \u2260 encoded as 3 UTF-8 bytes
564572

@@ -574,15 +582,14 @@ foreach (char c; b)
574582
writef("%x, ", c); // prints 'e2, 89, a0, '
575583
}
576584
--------------
577-
585+
)
578586

579587
$(P Aggregates can be string literals, which can be accessed
580588
as char, wchar, or dchar arrays:
581589
)
582590

583-
--------------
584-
void test()
585-
{
591+
$(SPEC_RUNNABLE_EXAMPLE_RUN
592+
--------------
586593
foreach (char c; "ab")
587594
{
588595
writefln("'%s'", c);
@@ -591,8 +598,8 @@ void test()
591598
{
592599
writefln("'%s'", w);
593600
}
594-
}
595-
--------------
601+
--------------
602+
)
596603

597604
$(P which would print:
598605
)
@@ -665,6 +672,7 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
665672

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

675+
$(SPEC_RUNNABLE_EXAMPLE_RUN
668676
--------------
669677
class Foo
670678
{
@@ -674,35 +682,33 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
674682
{
675683
int result = 0;
676684

677-
for (int i = 0; i < array.length; i++)
685+
foreach (e; array)
678686
{
679-
result = dg(array[i]);
687+
result = dg(e);
680688
if (result)
681689
break;
682690
}
683691
return result;
684692
}
685693
}
686-
--------------
687-
688-
$(P An example using this might be:)
689694

690-
--------------
691-
void test()
695+
void main()
692696
{
697+
import std.stdio;
693698
Foo a = new Foo();
694699

695700
a.array[0] = 73;
696701
a.array[1] = 82;
697702

698703
foreach (uint u; a)
699704
{
700-
writefln("%d", u);
705+
writeln(u);
701706
}
702707
}
703708
--------------
709+
)
704710

705-
$(P which would print:)
711+
$(P This would print:)
706712

707713
$(CONSOLE
708714
73
@@ -926,6 +932,8 @@ $(P
926932
and it is set to the index of each sequence element.
927933
)
928934
$(P Example:)
935+
936+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
929937
-----
930938
import std.meta : AliasSeq;
931939

@@ -939,6 +947,7 @@ void main()
939947
}
940948
}
941949
-----
950+
)
942951
$(P Output:)
943952

944953
$(CONSOLE
@@ -954,21 +963,20 @@ $(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
954963
$(P $(D ref) can be used to update the original elements:
955964
)
956965

957-
--------------
958-
void test()
959-
{
960-
static uint[2] a = [7, 8];
966+
$(SPEC_RUNNABLE_EXAMPLE_RUN
967+
--------------
968+
uint[2] a = [7, 8];
961969

962970
foreach (ref uint u; a)
963971
{
964972
u++;
965973
}
966974
foreach (uint u; a)
967975
{
968-
writefln("%d", u);
976+
writeln(u);
969977
}
970-
}
971-
--------------
978+
--------------
979+
)
972980

973981
which would print:
974982

@@ -1002,7 +1010,7 @@ foreach (int i; a)
10021010
a = null; // ok
10031011
--------------
10041012

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

10071015
$(P A foreach range statement loops over the specified range.)
10081016

@@ -1036,6 +1044,7 @@ $(GNAME ForeachRangeStatement):
10361044
is executed.
10371045
)
10381046

1047+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10391048
---
10401049
import std.stdio;
10411050

@@ -1053,6 +1062,7 @@ void main()
10531062
}
10541063
}
10551064
---
1065+
)
10561066

10571067
prints:
10581068

@@ -1061,14 +1071,6 @@ foo0123456789
10611071
)
10621072

10631073

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-
10721074
$(H2 $(LEGACY_LNAME2 SwitchStatement, switch-statement, Switch Statement))
10731075

10741076
A switch statement goes to one of a collection of case
@@ -1443,7 +1445,7 @@ $(GNAME WithStatement):
14431445
Within the with body the referenced object is searched first for
14441446
identifier symbols.
14451447

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

14481450
--------------
14491451
with (expression)
@@ -1487,7 +1489,7 @@ with (Foo)
14871489
}
14881490
--------------
14891491

1490-
$(P Use of with object symbols that shadow local symbols with
1492+
$(P Use of `with` object symbols that shadow local symbols with
14911493
the same identifier are not allowed.
14921494
This is to reduce the risk of inadvertent breakage of with
14931495
statements when new members are added to the object declaration.
@@ -1512,6 +1514,8 @@ void main()
15121514
$(P In nested $(I WithStatement)s, the inner-most scope takes precedence. If
15131515
a symbol cannot be resolved at the inner-most scope, resolution is forwarded
15141516
incrementally up the scope hierarchy.)
1517+
1518+
$(SPEC_RUNNABLE_EXAMPLE_RUN
15151519
---
15161520
import std.stdio;
15171521

@@ -1571,8 +1575,8 @@ void main()
15711575
// subsequently forward to module scope.
15721576
}
15731577
}
1574-
15751578
---
1579+
)
15761580

15771581
$(H2 $(LEGACY_LNAME2 SynchronizedStatement, synchronized-statement, Synchronized Statement))
15781582

@@ -1684,6 +1688,7 @@ $(GNAME FinallyStatement):
16841688
to the original exception (the head of the chain) if a bypass occurred,
16851689
so that the entire exception history is retained.)
16861690

1691+
$(SPEC_RUNNABLE_EXAMPLE_RUN
16871692
--------------
16881693
import std.stdio;
16891694

@@ -1709,6 +1714,7 @@ int main()
17091714
return 0;
17101715
}
17111716
--------------
1717+
)
17121718

17131719
prints:
17141720

@@ -1770,7 +1776,7 @@ $(PSCURLYSCOPE) when the scope exits due to exception unwinding.
17701776
scope, their destructions will be interleaved with the $(I ScopeGuardStatement)s
17711777
in the reverse lexical order in which they appear.)
17721778

1773-
1779+
$(SPEC_RUNNABLE_EXAMPLE_RUN
17741780
----
17751781
write("1");
17761782
{
@@ -1781,6 +1787,7 @@ write("1");
17811787
}
17821788
writeln();
17831789
----
1790+
)
17841791

17851792
writes:
17861793

@@ -1789,6 +1796,7 @@ $(CONSOLE
17891796
)
17901797

17911798

1799+
$(SPEC_RUNNABLE_EXAMPLE_RUN
17921800
----
17931801
{
17941802
scope(exit) write("1");
@@ -1798,14 +1806,15 @@ $(CONSOLE
17981806
}
17991807
writeln();
18001808
----
1809+
)
18011810

18021811
writes:
18031812

18041813
$(CONSOLE
18051814
4321
18061815
)
18071816

1808-
1817+
$(SPEC_RUNNABLE_EXAMPLE_RUN
18091818
----
18101819
struct Foo
18111820
{
@@ -1829,6 +1838,7 @@ catch (Exception e)
18291838
}
18301839
writeln();
18311840
----
1841+
)
18321842

18331843
writes:
18341844

@@ -1949,32 +1959,32 @@ $(GNAME MixinStatement):
19491959
$(GLINK StatementList), and is compiled as such.
19501960
)
19511961

1962+
$(SPEC_RUNNABLE_EXAMPLE_RUN
19521963
---
19531964
import std.stdio;
19541965

19551966
void main()
19561967
{
1957-
int j;
1968+
int i = 0;
19581969
mixin("
19591970
int x = 3;
1960-
for (int i = 0; i < 3; i++)
1961-
writeln(x + i, ++j);
1971+
for (; i < 3; i++)
1972+
writeln(x + i, i);
19621973
"); // ok
19631974

1964-
string s = "int y;";
1975+
enum s = "int y;";
19651976
mixin(s); // ok
19661977
y = 4; // ok, mixin declared y
19671978

19681979
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
1980+
//mixin(t); // error, t is not evaluatable at compile time
1981+
//mixin("y =") 4; // error, string must be complete statement
19721982

19731983
mixin("y =" ~ "4;"); // ok
1974-
19751984
mixin("y =", 2+2, ";"); // ok
19761985
}
19771986
---
1987+
)
19781988

19791989
$(SPEC_SUBNAV_PREV_NEXT expression, Expressions, arrays, Arrays)
19801990
)

0 commit comments

Comments
 (0)