Skip to content

Commit 6690433

Browse files
committed
[spec/version.dd] Improve Static If, Static Foreach sections
Fix identifier capitalization. Make some examples runnable. Simplify foreach iota example. Explain using double braces to create a scope with foreach.
1 parent 1ca5e4d commit 6690433

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

spec/version.dd

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -545,23 +545,26 @@ class C
545545
$(CODE_HIGHLIGHT static if) (k == 5) // ok, k is in current scope
546546
int z;
547547
}
548-
549-
template INT(int i)
548+
---
549+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
550+
---
551+
template Int(int i)
550552
{
551553
$(ARGS static if) (i == 32)
552-
alias INT = int;
554+
alias Int = int;
553555
$(ARGS else static if) (i == 16)
554-
alias INT = short;
556+
alias Int = short;
555557
$(ARGS else)
556558
static assert(0); // not supported
557559
}
558560

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
562564
------
565+
)
563566

564-
$(P A $(I StaticIfConditional) condition differs from an
567+
$(P A $(I StaticIfCondition) differs from an
565568
$(I IfStatement) in the following ways:
566569
)
567570

@@ -607,17 +610,19 @@ $(GNAME StaticForeachStatement):
607610
variables are never runtime variables.)
608611
)
609612

613+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
610614
------
611615
static foreach(i; [0, 1, 2, 3])
612616
{
613617
pragma(msg, i);
614618
}
615619
------
620+
)
616621

617622
$(P $(D static foreach) supports multiple variables in cases where the
618623
corresponding $(D foreach) statement supports them. (In this case,
619624
$(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).
621626
)
622627

623628
------
@@ -632,17 +637,35 @@ static foreach(i, v; ['a', 'b', 'c', 'd'])
632637
used to generate declarations:
633638
)
634639

640+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
635641
------
636642
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))
640645
{
641-
mixin(`enum x` ~ i ~ ` = i;`);
646+
mixin(`enum x`, i, ` = i;`);
642647
}
643648

644649
pragma(msg, x0, " ", x1," ", x2); // 0 1 2
645650
------
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`))
646669

647670
$(P As $(D static foreach) is a code generation construct and not a
648671
loop, $(D break) and $(D continue) cannot be used to change control
@@ -670,7 +693,7 @@ int test(int x)
670693

671694
static foreach(i; 0 .. 200)
672695
{
673-
static assert(test(i) == (i<100 ? i : -1));
696+
static assert(test(i) == (i < 100 ? i : -1));
674697
}
675698
-------
676699

@@ -711,7 +734,7 @@ $(GNAME StaticAssert):
711734
$(D static assert $(LPAREN)) $(GLINK2 expression, AssertArguments) $(D $(RPAREN);)
712735
)
713736

714-
$(P $(ASSIGNEXPRESSION) is evaluated at compile time, and converted
737+
$(P The first $(ASSIGNEXPRESSION) is evaluated at compile time, and converted
715738
to a boolean value. If the value is true, the static assert
716739
is ignored. If the value is false, an error diagnostic is issued
717740
and the compile fails.

0 commit comments

Comments
 (0)