Skip to content

Commit d8c36e0

Browse files
authored
[spec] Specify when opApply's delegate returns nonzero (#3814)
* [spec] Specify when opApply's delegate returns nonzero and add example about `break`
1 parent 721ac37 commit d8c36e0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

spec/statement.dd

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ $(GNAME OpApplyParameter):
695695
)
696696

697697
$(P where each $(I OpApplyParameter) of `dg` must match a $(GLINK ForeachType)
698-
in a *ForeachStatement*,
698+
in a $(GLINK ForeachStatement),
699699
otherwise the *ForeachStatement* will cause an error.)
700700

701701
$(P Any *ForeachTypeAttribute* cannot be `enum`.)
@@ -738,13 +738,18 @@ $(GNAME OpApplyParameter):
738738
apply must return 0.)
739739
)
740740

741+
$(P The result of calling the delegate will be nonzero if the *ForeachStatement*
742+
body executes a matching $(GLINK BreakStatement), $(GLINK ReturnStatement), or
743+
$(GLINK GotoStatement) whose matching label is outside the *ForeachStatement*.
744+
)
745+
741746
$(P For example, consider a class that is a container for two elements:)
742747

743748
$(SPEC_RUNNABLE_EXAMPLE_RUN
744749
--------------
745750
class Foo
746751
{
747-
uint[2] array;
752+
uint[] array;
748753

749754
int opApply(scope int delegate(ref uint) dg)
750755
{
@@ -763,10 +768,13 @@ $(GNAME OpApplyParameter):
763768
import std.stdio;
764769

765770
Foo a = new Foo();
766-
a.array = [73, 82];
771+
a.array = [73, 82, 2, 9];
767772

768773
foreach (uint u; a)
769774
{
775+
if (u < 5)
776+
break;
777+
770778
writeln(u);
771779
}
772780
}

0 commit comments

Comments
 (0)