Skip to content

Commit b8dddee

Browse files
authored
Merge pull request #4233 from ntrel/static-foreach
[spec/version] Improve static foreach docs
2 parents 21e4cb5 + 83ca888 commit b8dddee

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

spec/version.dd

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -597,21 +597,26 @@ $(GNAME StaticForeachStatement):
597597
$(GLINK StaticForeach) $(GLINK2 statement, NoScopeNonEmptyStatement)
598598
)
599599

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
601605
turned into a sequence of compile-time entities by evaluating
602606
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
604609
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)
607613
variable declaration (for constants) or an $(D alias)
608614
declaration (for symbols). (In particular, $(D static foreach)
609615
variables are never runtime variables.)
610-
)
611616

612617
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
613618
------
614-
static foreach(i; [0, 1, 2, 3])
619+
static foreach (i; [0, 1, 2, 3])
615620
{
616621
pragma(msg, i);
617622
}
@@ -624,23 +629,25 @@ static foreach(i; [0, 1, 2, 3])
624629
tuples are subsequently unpacked during iteration).
625630
)
626631

632+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
627633
------
628-
static foreach(i, v; ['a', 'b', 'c', 'd'])
634+
static foreach (i, v; ['a', 'b', 'c', 'd'])
629635
{
630636
static assert(i + 'a' == v);
631637
}
632638
------
639+
)
633640

634641
$(P Like bodies of $(GLINK ConditionalDeclaration)s, a $(D static foreach)
635642
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:
637644
)
638645

639646
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
640647
------
641648
import std.range : iota;
642649

643-
static foreach(i; iota(0, 3))
650+
static foreach (i; iota(0, 3))
644651
{
645652
mixin(`enum x`, i, ` = i;`);
646653
}
@@ -656,7 +663,7 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
656663
---
657664
void fun()
658665
{
659-
static foreach(s; ["hi", "hey", "hello"])
666+
static foreach (s; ["hi", "hey", "hello"])
660667
{{
661668
enum len = s.length; // local to each iteration
662669
static assert(len <= 5);
@@ -667,6 +674,9 @@ void fun()
667674
---
668675
)
669676

677+
$(P `static foreach` supports sequence expansion
678+
$(DDSUBLINK spec/statement, foreach_over_tuples, like `foreach`).)
679+
670680
$(H3 $(LNAME2 break-continue, `break` and `continue`))
671681

672682
$(P As $(D static foreach) is a code generation construct and not a
@@ -682,7 +692,7 @@ int test(int x)
682692
int r = -1;
683693
switch(x)
684694
{
685-
static foreach(i; 0 .. 100)
695+
static foreach (i; 0 .. 5)
686696
{
687697
case i:
688698
r = i;
@@ -693,9 +703,9 @@ int test(int x)
693703
return r;
694704
}
695705

696-
static foreach(i; 0 .. 200)
706+
static foreach (i; 0 .. 10)
697707
{
698-
static assert(test(i) == (i < 100 ? i : -1));
708+
static assert(test(i) == (i < 5 ? i : -1));
699709
}
700710
-------
701711

@@ -705,13 +715,14 @@ static foreach(i; 0 .. 200)
705715
labeled.)
706716
)
707717

718+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
708719
-------
709720
int test(int x)
710721
{
711722
int r = -1;
712723
Lswitch: switch(x)
713724
{
714-
static foreach(i; 0 .. 100)
725+
static foreach (i; 0 .. 5)
715726
{
716727
case i:
717728
r = i;
@@ -722,11 +733,12 @@ int test(int x)
722733
return r;
723734
}
724735

725-
static foreach(i; 0 .. 200)
736+
static foreach (i; 0 .. 10)
726737
{
727-
static assert(test(i) == (i<100 ? i : -1));
738+
static assert(test(i) == (i < 5 ? i : -1));
728739
}
729740
-------
741+
)
730742

731743

732744
$(H2 $(LEGACY_LNAME2 StaticAssert, static-assert, Static Assert))

0 commit comments

Comments
 (0)