Skip to content

Commit

Permalink
[spec] Specify when opApply's delegate returns nonzero (#3814)
Browse files Browse the repository at this point in the history
* [spec] Specify when opApply's delegate returns nonzero and add example about `break`
  • Loading branch information
ntrel authored Apr 27, 2024
1 parent 721ac37 commit d8c36e0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions spec/statement.dd
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ $(GNAME OpApplyParameter):
)

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

$(P Any *ForeachTypeAttribute* cannot be `enum`.)
Expand Down Expand Up @@ -738,13 +738,18 @@ $(GNAME OpApplyParameter):
apply must return 0.)
)

$(P The result of calling the delegate will be nonzero if the *ForeachStatement*
body executes a matching $(GLINK BreakStatement), $(GLINK ReturnStatement), or
$(GLINK GotoStatement) whose matching label is outside the *ForeachStatement*.
)

$(P For example, consider a class that is a container for two elements:)

$(SPEC_RUNNABLE_EXAMPLE_RUN
--------------
class Foo
{
uint[2] array;
uint[] array;

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

Foo a = new Foo();
a.array = [73, 82];
a.array = [73, 82, 2, 9];

foreach (uint u; a)
{
if (u < 5)
break;

writeln(u);
}
}
Expand Down

0 comments on commit d8c36e0

Please sign in to comment.