@@ -545,23 +545,26 @@ class C
545
545
$(CODE_HIGHLIGHT static if) (k == 5) // ok, k is in current scope
546
546
int z;
547
547
}
548
-
549
- template INT(int i)
548
+ ---
549
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
550
+ ---
551
+ template Int(int i)
550
552
{
551
553
$(ARGS static if) (i == 32)
552
- alias INT = int;
554
+ alias Int = int;
553
555
$(ARGS else static if) (i == 16)
554
- alias INT = short;
556
+ alias Int = short;
555
557
$(ARGS else)
556
558
static assert(0); // not supported
557
559
}
558
560
559
- INT !(32) a; // a is an int
560
- INT !(16) b; // b is a short
561
- INT !(17) c; // error, static assert trips
561
+ Int !(32) a; // a is an int
562
+ Int !(16) b; // b is a short
563
+ Int !(17) c; // error, static assert trips
562
564
------
565
+ )
563
566
564
- $(P A $(I StaticIfConditional) condition differs from an
567
+ $(P A $(I StaticIfCondition) differs from an
565
568
$(I IfStatement) in the following ways:
566
569
)
567
570
@@ -607,17 +610,19 @@ $(GNAME StaticForeachStatement):
607
610
variables are never runtime variables.)
608
611
)
609
612
613
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
610
614
------
611
615
static foreach(i; [0, 1, 2, 3])
612
616
{
613
617
pragma(msg, i);
614
618
}
615
619
------
620
+ )
616
621
617
622
$(P $(D static foreach) supports multiple variables in cases where the
618
623
corresponding $(D foreach) statement supports them. (In this case,
619
624
$(D static foreach) generates a compile-time sequence of tuples, and the
620
- tuples are subsequently unpacked during iteration.)
625
+ tuples are subsequently unpacked during iteration).
621
626
)
622
627
623
628
------
@@ -632,17 +637,35 @@ static foreach(i, v; ['a', 'b', 'c', 'd'])
632
637
used to generate declarations:
633
638
)
634
639
640
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
635
641
------
636
642
import std.range : iota;
637
- import std.algorithm : map;
638
- import std.conv : text;
639
- static foreach(i; iota(0, 3).map!text)
643
+
644
+ static foreach(i; iota(0, 3))
640
645
{
641
- mixin(`enum x` ~ i ~ ` = i;`);
646
+ mixin(`enum x`, i, ` = i;`);
642
647
}
643
648
644
649
pragma(msg, x0, " ", x1," ", x2); // 0 1 2
645
650
------
651
+ )
652
+
653
+ $(P If a new scope is desired for each expansion, use another
654
+ set of braces:)
655
+
656
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
657
+ ---
658
+ static foreach(s; ["hi", "hey", "hello"])
659
+ {{
660
+ enum len = s.length; // local to each iteration
661
+ static assert(len <= 5);
662
+ }}
663
+
664
+ static assert(!is(typeof(len)));
665
+ ---
666
+ )
667
+
668
+ $(H3 $(LNAME2 break-continue, `break` and `continue`))
646
669
647
670
$(P As $(D static foreach) is a code generation construct and not a
648
671
loop, $(D break) and $(D continue) cannot be used to change control
@@ -670,7 +693,7 @@ int test(int x)
670
693
671
694
static foreach(i; 0 .. 200)
672
695
{
673
- static assert(test(i) == (i< 100 ? i : -1));
696
+ static assert(test(i) == (i < 100 ? i : -1));
674
697
}
675
698
-------
676
699
@@ -711,7 +734,7 @@ $(GNAME StaticAssert):
711
734
$(D static assert $(LPAREN)) $(GLINK2 expression, AssertArguments) $(D $(RPAREN);)
712
735
)
713
736
714
- $(P $(ASSIGNEXPRESSION) is evaluated at compile time, and converted
737
+ $(P The first $(ASSIGNEXPRESSION) is evaluated at compile time, and converted
715
738
to a boolean value. If the value is true, the static assert
716
739
is ignored. If the value is false, an error diagnostic is issued
717
740
and the compile fails.
0 commit comments