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,17 @@ $(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
+ import std.stdio;
529
+ char[] a = ['h', 'i'];
530
+
531
+ foreach (size_t i, char c; a)
525
532
{
526
533
writefln("a[%d] = '%c'", i, c);
527
534
}
528
535
--------------
536
+ )
529
537
530
538
$(P For $(D foreach), the
531
539
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
559
567
can be decoded into any UTF type:
560
568
)
561
569
570
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
562
571
--------------
572
+ import std.stdio;
563
573
char[] a = "\xE2\x89\xA0".dup; // \u2260 encoded as 3 UTF-8 bytes
564
574
565
575
foreach (dchar c; a)
@@ -574,15 +584,16 @@ foreach (char c; b)
574
584
writef("%x, ", c); // prints 'e2, 89, a0, '
575
585
}
576
586
--------------
577
-
587
+ )
578
588
579
589
$(P Aggregates can be string literals, which can be accessed
580
590
as char, wchar, or dchar arrays:
581
591
)
582
592
583
- --------------
584
- void test()
585
- {
593
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
594
+ --------------
595
+ import std.stdio;
596
+
586
597
foreach (char c; "ab")
587
598
{
588
599
writefln("'%s'", c);
@@ -591,8 +602,8 @@ void test()
591
602
{
592
603
writefln("'%s'", w);
593
604
}
594
- }
595
- --------------
605
+ --------------
606
+ )
596
607
597
608
$(P which would print:
598
609
)
@@ -665,6 +676,7 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
665
676
666
677
$(P For example, consider a class that is a container for two elements:)
667
678
679
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
668
680
--------------
669
681
class Foo
670
682
{
@@ -674,35 +686,33 @@ $(H3 $(LNAME2 foreach_over_struct_and_classes, Foreach over Structs and Classes
674
686
{
675
687
int result = 0;
676
688
677
- for (int i = 0; i < array.length; i++ )
689
+ foreach (e; array)
678
690
{
679
- result = dg(array[i] );
691
+ result = dg(e );
680
692
if (result)
681
693
break;
682
694
}
683
695
return result;
684
696
}
685
697
}
686
- --------------
687
-
688
- $(P An example using this might be:)
689
698
690
- --------------
691
- void test()
699
+ void main()
692
700
{
701
+ import std.stdio;
693
702
Foo a = new Foo();
694
703
695
704
a.array[0] = 73;
696
705
a.array[1] = 82;
697
706
698
707
foreach (uint u; a)
699
708
{
700
- writefln("%d", u);
709
+ writeln( u);
701
710
}
702
711
}
703
712
--------------
713
+ )
704
714
705
- $(P which would print:)
715
+ $(P This would print:)
706
716
707
717
$(CONSOLE
708
718
73
926
936
and it is set to the index of each sequence element.
927
937
)
928
938
$(P Example:)
939
+
940
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
929
941
-----
930
942
import std.meta : AliasSeq;
931
943
@@ -939,6 +951,7 @@ void main()
939
951
}
940
952
}
941
953
-----
954
+ )
942
955
$(P Output:)
943
956
944
957
$(CONSOLE
@@ -954,21 +967,21 @@ $(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
954
967
$(P $(D ref) can be used to update the original elements:
955
968
)
956
969
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];
961
974
962
975
foreach (ref uint u; a)
963
976
{
964
977
u++;
965
978
}
966
979
foreach (uint u; a)
967
980
{
968
- writefln("%d", u);
981
+ writeln( u);
969
982
}
970
- }
971
- --------------
983
+ --------------
984
+ )
972
985
973
986
which would print:
974
987
@@ -1002,7 +1015,7 @@ foreach (int i; a)
1002
1015
a = null; // ok
1003
1016
--------------
1004
1017
1005
- $(H2 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
1018
+ $(H3 $(LEGACY_LNAME2 ForeachRangeStatement, foreach-range-statement, Foreach Range Statement))
1006
1019
1007
1020
$(P A foreach range statement loops over the specified range.)
1008
1021
@@ -1036,6 +1049,7 @@ $(GNAME ForeachRangeStatement):
1036
1049
is executed.
1037
1050
)
1038
1051
1052
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1039
1053
---
1040
1054
import std.stdio;
1041
1055
@@ -1053,6 +1067,7 @@ void main()
1053
1067
}
1054
1068
}
1055
1069
---
1070
+ )
1056
1071
1057
1072
prints:
1058
1073
@@ -1061,14 +1076,6 @@ foo0123456789
1061
1076
)
1062
1077
1063
1078
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
1079
$(H2 $(LEGACY_LNAME2 SwitchStatement, switch-statement, Switch Statement))
1073
1080
1074
1081
A switch statement goes to one of a collection of case
@@ -1443,7 +1450,7 @@ $(GNAME WithStatement):
1443
1450
Within the with body the referenced object is searched first for
1444
1451
identifier symbols.
1445
1452
1446
- $(P The $(I WithStatement))
1453
+ $(P Below, if `ident` is a member of the type of `expression`, the $(I WithStatement): )
1447
1454
1448
1455
--------------
1449
1456
with (expression)
@@ -1487,7 +1494,7 @@ with (Foo)
1487
1494
}
1488
1495
--------------
1489
1496
1490
- $(P Use of with object symbols that shadow local symbols with
1497
+ $(P Use of ` with` object symbols that shadow local symbols with
1491
1498
the same identifier are not allowed.
1492
1499
This is to reduce the risk of inadvertent breakage of with
1493
1500
statements when new members are added to the object declaration.
@@ -1512,6 +1519,8 @@ void main()
1512
1519
$(P In nested $(I WithStatement)s, the inner-most scope takes precedence. If
1513
1520
a symbol cannot be resolved at the inner-most scope, resolution is forwarded
1514
1521
incrementally up the scope hierarchy.)
1522
+
1523
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1515
1524
---
1516
1525
import std.stdio;
1517
1526
@@ -1571,8 +1580,8 @@ void main()
1571
1580
// subsequently forward to module scope.
1572
1581
}
1573
1582
}
1574
-
1575
1583
---
1584
+ )
1576
1585
1577
1586
$(H2 $(LEGACY_LNAME2 SynchronizedStatement, synchronized-statement, Synchronized Statement))
1578
1587
@@ -1684,6 +1693,7 @@ $(GNAME FinallyStatement):
1684
1693
to the original exception (the head of the chain) if a bypass occurred,
1685
1694
so that the entire exception history is retained.)
1686
1695
1696
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1687
1697
--------------
1688
1698
import std.stdio;
1689
1699
@@ -1709,6 +1719,7 @@ int main()
1709
1719
return 0;
1710
1720
}
1711
1721
--------------
1722
+ )
1712
1723
1713
1724
prints:
1714
1725
@@ -1770,8 +1781,10 @@ $(PSCURLYSCOPE) when the scope exits due to exception unwinding.
1770
1781
scope, their destructions will be interleaved with the $(I ScopeGuardStatement)s
1771
1782
in the reverse lexical order in which they appear.)
1772
1783
1773
-
1784
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1774
1785
----
1786
+ import std.stdio;
1787
+
1775
1788
write("1");
1776
1789
{
1777
1790
write("2");
@@ -1781,6 +1794,7 @@ write("1");
1781
1794
}
1782
1795
writeln();
1783
1796
----
1797
+ )
1784
1798
1785
1799
writes:
1786
1800
@@ -1789,7 +1803,9 @@ $(CONSOLE
1789
1803
)
1790
1804
1791
1805
1806
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1792
1807
----
1808
+ import std.stdio;
1793
1809
{
1794
1810
scope(exit) write("1");
1795
1811
scope(success) write("2");
@@ -1798,15 +1814,18 @@ $(CONSOLE
1798
1814
}
1799
1815
writeln();
1800
1816
----
1817
+ )
1801
1818
1802
1819
writes:
1803
1820
1804
1821
$(CONSOLE
1805
1822
4321
1806
1823
)
1807
1824
1808
-
1825
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1809
1826
----
1827
+ import std.stdio;
1828
+
1810
1829
struct Foo
1811
1830
{
1812
1831
this(string s) { write(s); }
@@ -1829,6 +1848,7 @@ catch (Exception e)
1829
1848
}
1830
1849
writeln();
1831
1850
----
1851
+ )
1832
1852
1833
1853
writes:
1834
1854
@@ -1949,32 +1969,32 @@ $(GNAME MixinStatement):
1949
1969
$(GLINK StatementList), and is compiled as such.
1950
1970
)
1951
1971
1972
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1952
1973
---
1953
1974
import std.stdio;
1954
1975
1955
1976
void main()
1956
1977
{
1957
- int j ;
1978
+ int i = 0 ;
1958
1979
mixin("
1959
1980
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 );
1962
1983
"); // ok
1963
1984
1964
- string s = "int y;";
1985
+ enum s = "int y;";
1965
1986
mixin(s); // ok
1966
1987
y = 4; // ok, mixin declared y
1967
1988
1968
1989
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
1972
1992
1973
1993
mixin("y =" ~ "4;"); // ok
1974
-
1975
1994
mixin("y =", 2+2, ";"); // ok
1976
1995
}
1977
1996
---
1997
+ )
1978
1998
1979
1999
$(SPEC_SUBNAV_PREV_NEXT expression, Expressions, arrays, Arrays)
1980
2000
)
0 commit comments