501
501
in the $(PS0).
502
502
)
503
503
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
+
504
509
$(H3 $(LNAME2 foreach_over_arrays, Foreach over Arrays))
505
510
506
511
$(P
@@ -518,14 +523,16 @@ $(P
518
523
$(I index) cannot be `ref`.
519
524
It is set to be the index of the array element.
520
525
)
526
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
521
527
--------------
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)
525
531
{
526
532
writefln("a[%d] = '%c'", i, c);
527
533
}
528
534
--------------
535
+ )
529
536
530
537
$(P For $(D foreach), the
531
538
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
559
566
can be decoded into any UTF type:
560
567
)
561
568
569
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
562
570
--------------
563
571
char[] a = "\xE2\x89\xA0".dup; // \u2260 encoded as 3 UTF-8 bytes
564
572
@@ -574,15 +582,14 @@ foreach (char c; b)
574
582
writef("%x, ", c); // prints 'e2, 89, a0, '
575
583
}
576
584
--------------
577
-
585
+ )
578
586
579
587
$(P Aggregates can be string literals, which can be accessed
580
588
as char, wchar, or dchar arrays:
581
589
)
582
590
583
- --------------
584
- void test()
585
- {
591
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
592
+ --------------
586
593
foreach (char c; "ab")
587
594
{
588
595
writefln("'%s'", c);
@@ -591,8 +598,8 @@ void test()
591
598
{
592
599
writefln("'%s'", w);
593
600
}
594
- }
595
- --------------
601
+ --------------
602
+ )
596
603
597
604
$(P which would print:
598
605
)
@@ -665,6 +672,7 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
665
672
666
673
$(P For example, consider a class that is a container for two elements:)
667
674
675
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
668
676
--------------
669
677
class Foo
670
678
{
@@ -674,35 +682,33 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
674
682
{
675
683
int result = 0;
676
684
677
- for (int i = 0; i < array.length; i++ )
685
+ foreach (e; array)
678
686
{
679
- result = dg(array[i] );
687
+ result = dg(e );
680
688
if (result)
681
689
break;
682
690
}
683
691
return result;
684
692
}
685
693
}
686
- --------------
687
-
688
- $(P An example using this might be:)
689
694
690
- --------------
691
- void test()
695
+ void main()
692
696
{
697
+ import std.stdio;
693
698
Foo a = new Foo();
694
699
695
700
a.array[0] = 73;
696
701
a.array[1] = 82;
697
702
698
703
foreach (uint u; a)
699
704
{
700
- writefln("%d", u);
705
+ writeln( u);
701
706
}
702
707
}
703
708
--------------
709
+ )
704
710
705
- $(P which would print:)
711
+ $(P This would print:)
706
712
707
713
$(CONSOLE
708
714
73
926
932
and it is set to the index of each sequence element.
927
933
)
928
934
$(P Example:)
935
+
936
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
929
937
-----
930
938
import std.meta : AliasSeq;
931
939
@@ -939,6 +947,7 @@ void main()
939
947
}
940
948
}
941
949
-----
950
+ )
942
951
$(P Output:)
943
952
944
953
$(CONSOLE
@@ -954,21 +963,20 @@ $(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
954
963
$(P $(D ref) can be used to update the original elements:
955
964
)
956
965
957
- --------------
958
- void test()
959
- {
960
- static uint[2] a = [7, 8];
966
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
967
+ --------------
968
+ uint[2] a = [7, 8];
961
969
962
970
foreach (ref uint u; a)
963
971
{
964
972
u++;
965
973
}
966
974
foreach (uint u; a)
967
975
{
968
- writefln("%d", u);
976
+ writeln( u);
969
977
}
970
- }
971
- --------------
978
+ --------------
979
+ )
972
980
973
981
which would print:
974
982
@@ -1002,7 +1010,7 @@ foreach (int i; a)
1002
1010
a = null; // ok
1003
1011
--------------
1004
1012
1005
- $(H2 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
1013
+ $(H3 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
1006
1014
1007
1015
$(P A foreach range statement loops over the specified range.)
1008
1016
@@ -1036,6 +1044,7 @@ $(GNAME ForeachRangeStatement):
1036
1044
is executed.
1037
1045
)
1038
1046
1047
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1039
1048
---
1040
1049
import std.stdio;
1041
1050
@@ -1053,6 +1062,7 @@ void main()
1053
1062
}
1054
1063
}
1055
1064
---
1065
+ )
1056
1066
1057
1067
prints:
1058
1068
@@ -1061,14 +1071,6 @@ foo0123456789
1061
1071
)
1062
1072
1063
1073
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
-
1072
1074
$(H2 $(LEGACY_LNAME2 SwitchStatement, switch-statement, Switch Statement))
1073
1075
1074
1076
A switch statement goes to one of a collection of case
@@ -1443,7 +1445,7 @@ $(GNAME WithStatement):
1443
1445
Within the with body the referenced object is searched first for
1444
1446
identifier symbols.
1445
1447
1446
- $(P The $(I WithStatement))
1448
+ $(P Below, if `ident` is a member of the type of `expression`, the $(I WithStatement): )
1447
1449
1448
1450
--------------
1449
1451
with (expression)
@@ -1487,7 +1489,7 @@ with (Foo)
1487
1489
}
1488
1490
--------------
1489
1491
1490
- $(P Use of with object symbols that shadow local symbols with
1492
+ $(P Use of ` with` object symbols that shadow local symbols with
1491
1493
the same identifier are not allowed.
1492
1494
This is to reduce the risk of inadvertent breakage of with
1493
1495
statements when new members are added to the object declaration.
@@ -1512,6 +1514,8 @@ void main()
1512
1514
$(P In nested $(I WithStatement)s, the inner-most scope takes precedence. If
1513
1515
a symbol cannot be resolved at the inner-most scope, resolution is forwarded
1514
1516
incrementally up the scope hierarchy.)
1517
+
1518
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1515
1519
---
1516
1520
import std.stdio;
1517
1521
@@ -1571,8 +1575,8 @@ void main()
1571
1575
// subsequently forward to module scope.
1572
1576
}
1573
1577
}
1574
-
1575
1578
---
1579
+ )
1576
1580
1577
1581
$(H2 $(LEGACY_LNAME2 SynchronizedStatement, synchronized-statement, Synchronized Statement))
1578
1582
@@ -1684,6 +1688,7 @@ $(GNAME FinallyStatement):
1684
1688
to the original exception (the head of the chain) if a bypass occurred,
1685
1689
so that the entire exception history is retained.)
1686
1690
1691
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1687
1692
--------------
1688
1693
import std.stdio;
1689
1694
@@ -1709,6 +1714,7 @@ int main()
1709
1714
return 0;
1710
1715
}
1711
1716
--------------
1717
+ )
1712
1718
1713
1719
prints:
1714
1720
@@ -1770,7 +1776,7 @@ $(PSCURLYSCOPE) when the scope exits due to exception unwinding.
1770
1776
scope, their destructions will be interleaved with the $(I ScopeGuardStatement)s
1771
1777
in the reverse lexical order in which they appear.)
1772
1778
1773
-
1779
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1774
1780
----
1775
1781
write("1");
1776
1782
{
@@ -1781,6 +1787,7 @@ write("1");
1781
1787
}
1782
1788
writeln();
1783
1789
----
1790
+ )
1784
1791
1785
1792
writes:
1786
1793
@@ -1789,6 +1796,7 @@ $(CONSOLE
1789
1796
)
1790
1797
1791
1798
1799
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1792
1800
----
1793
1801
{
1794
1802
scope(exit) write("1");
@@ -1798,14 +1806,15 @@ $(CONSOLE
1798
1806
}
1799
1807
writeln();
1800
1808
----
1809
+ )
1801
1810
1802
1811
writes:
1803
1812
1804
1813
$(CONSOLE
1805
1814
4321
1806
1815
)
1807
1816
1808
-
1817
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1809
1818
----
1810
1819
struct Foo
1811
1820
{
@@ -1829,6 +1838,7 @@ catch (Exception e)
1829
1838
}
1830
1839
writeln();
1831
1840
----
1841
+ )
1832
1842
1833
1843
writes:
1834
1844
@@ -1949,32 +1959,32 @@ $(GNAME MixinStatement):
1949
1959
$(GLINK StatementList), and is compiled as such.
1950
1960
)
1951
1961
1962
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1952
1963
---
1953
1964
import std.stdio;
1954
1965
1955
1966
void main()
1956
1967
{
1957
- int j ;
1968
+ int i = 0 ;
1958
1969
mixin("
1959
1970
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 );
1962
1973
"); // ok
1963
1974
1964
- string s = "int y;";
1975
+ enum s = "int y;";
1965
1976
mixin(s); // ok
1966
1977
y = 4; // ok, mixin declared y
1967
1978
1968
1979
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
1972
1982
1973
1983
mixin("y =" ~ "4;"); // ok
1974
-
1975
1984
mixin("y =", 2+2, ";"); // ok
1976
1985
}
1977
1986
---
1987
+ )
1978
1988
1979
1989
$(SPEC_SUBNAV_PREV_NEXT expression, Expressions, arrays, Arrays)
1980
1990
)
0 commit comments