Skip to content

Commit 592cc72

Browse files
authored
[spec/statement] Define alias and enum on sequence foreach (#4279)
1 parent 0ec7f81 commit 592cc72

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

spec/statement.dd

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,25 +1105,25 @@ $(P
11051105
on a sequence, though there is a separate scope for each expansion.
11061106
)
11071107
$(P There
1108-
can be one or two iteration symbols declared. If one, then the symbol
1109-
is an $(I element alias) of each element in the sequence in turn.
1108+
can be one or two iteration symbols declared. If one, then it's
1109+
an $(I element symbol) representing each element in the sequence in turn.
11101110
)
11111111
$(UL
11121112
$(LI
1113-
If the sequence is a $(I TypeSeq), the element alias is set to each
1113+
If the sequence is a $(I TypeSeq), the element symbol is set to each
11141114
type in turn.
11151115
)$(LI
1116-
If the sequence is a $(I ValueSeq), the element alias
1117-
is set to each value in turn. If the type of the element alias
1116+
If the sequence is a $(I ValueSeq), the element symbol
1117+
is set to each value in turn. If the type of the element symbol
11181118
is given, it must be compatible with the type of every sequence element.
1119-
If no type is given, the type of the element alias will match the type
1119+
If no type is given, the type of the element symbol will match the type
11201120
of each sequence element, which may change between elements.
11211121
))
11221122
$(P
11231123
If there are
1124-
two symbols declared, the first is the $(I index variable)
1125-
and the second is the $(I element alias). The index
1126-
must be of `int`, `uint`, `long` or `ulong` type,
1124+
two symbols declared, the first is the $(I index)
1125+
and the second is the $(I element). The index symbol
1126+
must be of `int`, `uint`, `long` or `ulong` type (if specified),
11271127
it cannot be `ref`,
11281128
and it is set to the index of each sequence element.
11291129
)
@@ -1178,6 +1178,46 @@ hi has type string
11781178
[2, 5] has type int[]
11791179
)
11801180

1181+
$(H4 $(LNAME2 foreach_sequence_kinds, Symbol Kinds))
1182+
1183+
$(P Each $(GLINK ForeachType) declaration (without `alias`/`enum`)
1184+
will attempt to generate:)
1185+
1186+
* Compile-time symbols for values known at compile-time
1187+
* Runtime variables for elements with values not known at compile-time
1188+
* Symbolic aliases for elements that don't have values (e.g types)
1189+
1190+
$(P `alias` or `enum` can be specified in a *ForeachType*:)
1191+
1192+
* The `alias` storage class will create a symbolic alias
1193+
* The `enum` storage class will create a compile-time constant
1194+
1195+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1196+
---
1197+
void main()
1198+
{
1199+
import std.meta : AliasSeq;
1200+
1201+
int i = 1;
1202+
1203+
// without `alias`, `a` would be a separate variable initialized from `i`
1204+
foreach (alias a; AliasSeq!(i, i))
1205+
{
1206+
a++;
1207+
}
1208+
assert(i == 3);
1209+
1210+
foreach (e; AliasSeq!(i, 1))
1211+
{
1212+
static if (__traits(compiles, &e))
1213+
assert(e == 3); // first e is a runtime variable
1214+
else
1215+
static assert(e == 1); // second e is a compile-time constant
1216+
}
1217+
}
1218+
---
1219+
)
1220+
11811221
$(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
11821222

11831223
$(P $(D ref) can be used to modify the elements of the *ForeachAggregate*.

0 commit comments

Comments
 (0)