@@ -597,21 +597,26 @@ $(GNAME StaticForeachStatement):
597
597
$(GLINK StaticForeach) $(GLINK2 statement, NoScopeNonEmptyStatement)
598
598
)
599
599
600
- $(P The aggregate/range bounds are evaluated at compile time and
600
+ $(P `static foreach` expands its *DeclarationBlock* or *DeclDefs* into a
601
+ series of declarations, each of which may reference any
602
+ $(GLINK2 statement, ForeachType) symbols declared.)
603
+
604
+ - The aggregate/range bounds are evaluated at compile time and
601
605
turned into a sequence of compile-time entities by evaluating
602
606
corresponding code with a $(GLINK2 statement, ForeachStatement)/$(GLINK2 statement, ForeachRangeStatement)
603
- at compile time. The body of the $(D static foreach) is then copied a
607
+ at compile time.
608
+ - The body of the $(D static foreach) is then copied a
604
609
number of times that corresponds to the number of elements of the
605
- sequence. Within the i-th copy, the name of the $(D static foreach)
606
- variable is bound to the i-th entry of the sequence, either as an $(D enum)
610
+ sequence.
611
+ - Within the i-th copy, the name of the $(D static foreach) element
612
+ 'variable' is bound to the i-th entry of the sequence, either as an $(D enum)
607
613
variable declaration (for constants) or an $(D alias)
608
614
declaration (for symbols). (In particular, $(D static foreach)
609
615
variables are never runtime variables.)
610
- )
611
616
612
617
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
613
618
------
614
- static foreach(i; [0, 1, 2, 3])
619
+ static foreach (i; [0, 1, 2, 3])
615
620
{
616
621
pragma(msg, i);
617
622
}
@@ -624,23 +629,25 @@ static foreach(i; [0, 1, 2, 3])
624
629
tuples are subsequently unpacked during iteration).
625
630
)
626
631
632
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
627
633
------
628
- static foreach(i, v; ['a', 'b', 'c', 'd'])
634
+ static foreach (i, v; ['a', 'b', 'c', 'd'])
629
635
{
630
636
static assert(i + 'a' == v);
631
637
}
632
638
------
639
+ )
633
640
634
641
$(P Like bodies of $(GLINK ConditionalDeclaration)s, a $(D static foreach)
635
642
body does not introduce a new scope. Therefore, it can be
636
- used to generate declarations:
643
+ used to add declarations to an existing scope :
637
644
)
638
645
639
646
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
640
647
------
641
648
import std.range : iota;
642
649
643
- static foreach(i; iota(0, 3))
650
+ static foreach (i; iota(0, 3))
644
651
{
645
652
mixin(`enum x`, i, ` = i;`);
646
653
}
@@ -656,7 +663,7 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
656
663
---
657
664
void fun()
658
665
{
659
- static foreach(s; ["hi", "hey", "hello"])
666
+ static foreach (s; ["hi", "hey", "hello"])
660
667
{{
661
668
enum len = s.length; // local to each iteration
662
669
static assert(len <= 5);
@@ -667,6 +674,9 @@ void fun()
667
674
---
668
675
)
669
676
677
+ $(P `static foreach` supports sequence expansion
678
+ $(DDSUBLINK spec/statement, foreach_over_tuples, like `foreach`).)
679
+
670
680
$(H3 $(LNAME2 break-continue, `break` and `continue`))
671
681
672
682
$(P As $(D static foreach) is a code generation construct and not a
@@ -682,7 +692,7 @@ int test(int x)
682
692
int r = -1;
683
693
switch(x)
684
694
{
685
- static foreach(i; 0 .. 100 )
695
+ static foreach (i; 0 .. 5 )
686
696
{
687
697
case i:
688
698
r = i;
@@ -693,9 +703,9 @@ int test(int x)
693
703
return r;
694
704
}
695
705
696
- static foreach(i; 0 .. 200 )
706
+ static foreach (i; 0 .. 10 )
697
707
{
698
- static assert(test(i) == (i < 100 ? i : -1));
708
+ static assert(test(i) == (i < 5 ? i : -1));
699
709
}
700
710
-------
701
711
@@ -705,13 +715,14 @@ static foreach(i; 0 .. 200)
705
715
labeled.)
706
716
)
707
717
718
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
708
719
-------
709
720
int test(int x)
710
721
{
711
722
int r = -1;
712
723
Lswitch: switch(x)
713
724
{
714
- static foreach(i; 0 .. 100 )
725
+ static foreach (i; 0 .. 5 )
715
726
{
716
727
case i:
717
728
r = i;
@@ -722,11 +733,12 @@ int test(int x)
722
733
return r;
723
734
}
724
735
725
- static foreach(i; 0 .. 200 )
736
+ static foreach (i; 0 .. 10 )
726
737
{
727
- static assert(test(i) == (i<100 ? i : -1));
738
+ static assert(test(i) == (i < 5 ? i : -1));
728
739
}
729
740
-------
741
+ )
730
742
731
743
732
744
$(H2 $(LEGACY_LNAME2 StaticAssert, static-assert, Static Assert))
0 commit comments