Skip to content

Commit fe35ad5

Browse files
authored
Merge pull request #2248 from ntrel/foreach-seq
Improve *Foreach over Tuples* spec merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
2 parents 0dd3a2a + d47264d commit fe35ad5

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

spec/statement.dd

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ $(GNAME ForeachAggregate):
465465
$(P
466466
$(I ForeachAggregate) is evaluated. It must evaluate to an expression
467467
of type static array, dynamic array, associative array,
468-
struct, class, delegate, or tuple.
468+
struct, class, delegate, or sequence.
469469
The $(PS0) is executed, once for each element of the
470470
aggregate.
471471
At the start of each iteration, the variables declared by
@@ -492,8 +492,8 @@ $(P
492492
If there are
493493
two variables declared, the first is said to be the $(I index)
494494
and the second is said to be the $(I value). The $(I index)
495-
must be of $(D int), $(D uint) or $(D size_t) type,
496-
it cannot be $(I ref),
495+
must be of `int`, `uint`, `long` or `ulong` type,
496+
it cannot be `ref`,
497497
and it is set to be the index of the array element.
498498
)
499499
--------------
@@ -592,7 +592,7 @@ $(H3 $(LNAME2 foreach_over_associative_arrays, Foreach over Associative Arrays))
592592
two variables declared, the first is said to be the $(I index)
593593
and the second is said to be the $(I value). The $(I index)
594594
must be of the same type as the indexing type of the associative
595-
array. It cannot be $(I ref),
595+
array. It cannot be `ref`,
596596
and it is set to be the index of the array element.
597597
The order in which the elements of the array are iterated over is unspecified
598598
for $(D foreach). $(D foreach_reverse) for associative arrays
@@ -842,52 +842,54 @@ void main()
842842
that is confusing to read. Therefore, using $(D foreach_reverse) with a
843843
delegate is now deprecated, and will be rejected in the future.)
844844

845-
$(H3 $(LNAME2 foreach_over_tuples, Foreach over Tuples))
845+
$(H3 $(LNAME2 foreach_over_tuples, Foreach over Sequences))
846846

847847
$(P
848-
If the aggregate expression is a tuple, there
849-
can be one or two variables declared. If one, then the variable
850-
is said to be the $(I value) set to the elements of the tuple,
851-
one by one. If the type of the
852-
variable is given, it must match the type of the tuple contents.
853-
If it is not given, the type of the variable is set to the type
854-
of the tuple element, which may change from iteration to iteration.
855-
If there are
856-
two variables declared, the first is said to be the $(I index)
857-
and the second is said to be the $(I value). The $(I index)
858-
must be of $(D int) or $(D uint) type, it cannot be $(I ref),
859-
and it is set to be the index of the tuple element.
860-
)
861-
862-
$(P
863-
If the tuple is a list of types, then the foreach statement
864-
is executed once for each type, and the value is aliased to that
848+
If the aggregate expression is a sequence, there
849+
can be one or two iteration symbols declared. If one, then the symbol
850+
is an $(I element alias) of each element in the sequence in turn.
851+
)$(P
852+
If the sequence is a $(I TypeSeq), then the foreach statement
853+
is executed once for each type, and the element alias is set to each
865854
type.
855+
)$(P
856+
When the sequence is a $(I ValueSeq), the element alias is a variable
857+
and is set to each value in the sequence. If the type of the
858+
variable is given, it must match the type of the sequence contents.
859+
If no type is given, the type of the variable is set to the type
860+
of the sequence element, which may change from iteration to iteration.
861+
)$(P
862+
If there are
863+
two symbols declared, the first is the $(I index variable)
864+
and the second is the $(I element alias). The index
865+
must be of `int`, `uint`, `long` or `ulong` type,
866+
it cannot be `ref`,
867+
and it is set to the index of each sequence element.
866868
)
867-
869+
$(P Example:)
868870
-----
869-
import std.stdio;
870871
import std.meta : AliasSeq;
871872

872873
void main()
873874
{
874-
alias TL = AliasSeq!(int, long, double);
875+
alias Seq = AliasSeq!(int, "literal", main);
875876

876-
foreach (T; TL)
877+
foreach (sym; Seq)
877878
{
878-
writeln(typeid(T));
879+
pragma(msg, sym.stringof);
879880
}
880881
}
881882
-----
882-
883-
$(P Prints:)
883+
$(P Output:)
884884

885885
$(CONSOLE
886886
int
887-
long
888-
double
887+
"literal"
888+
main()
889889
)
890890

891+
$(P See also: $(DDSUBLINK spec/version, staticforeach, Static Foreach).)
892+
891893
$(H3 $(LNAME2 foreach_ref_parameters, Foreach Ref Parameters))
892894

893895
$(P $(D ref) can be used to update the original elements:

0 commit comments

Comments
 (0)