Skip to content

Commit d30930b

Browse files
ntreldlang-bot
authored andcommitted
[spec/statement.dd] Improve foreach over Delegates
1 parent 170cb33 commit d30930b

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

spec/statement.dd

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -917,14 +917,13 @@ $(H3 $(LNAME2 foreach_over_delegates, Foreach over Delegates))
917917
many different named looping strategies to coexist in the same
918918
class or struct.)
919919

920-
$(P For example:)
920+
$(P The delegate can generate the elements on the fly:)
921921

922-
--------------
923-
void main()
924-
{
922+
$(SPEC_RUNNABLE_EXAMPLE_RUN
923+
--------------
925924
// Custom loop implementation, that iterates over powers of 2 with
926-
// alternating sign. The loop body is passed in dg.
927-
int myLoop(int delegate(ref int) dg)
925+
// alternating sign. The foreach loop body is passed in dg.
926+
int myLoop(int delegate(int) dg)
928927
{
929928
for (int z = 1; z < 128; z *= -2)
930929
{
@@ -937,15 +936,15 @@ void main()
937936
return 0;
938937
}
939938

940-
// This example loop simply collects the loop index values into an array.
939+
// Append each value in the iteration to an array
941940
int[] result;
942-
foreach (ref x; &myLoop)
941+
foreach (x; &myLoop)
943942
{
944943
result ~= x;
945944
}
946945
assert(result == [1, -2, 4, -8, 16, -32, 64, -128]);
947-
}
948-
--------------
946+
--------------
947+
)
949948

950949
$(P $(B Note:) When $(I ForeachAggregate) is a delegate, the compiler
951950
does not try to implement reverse traversal of the results returned by

0 commit comments

Comments
 (0)